আপনার অ্যাপ থেকে কল ফাংশন


Firebase ক্লায়েন্ট SDK-এর জন্য ক্লাউড ফাংশন আপনাকে Firebase অ্যাপ থেকে সরাসরি ফাংশন কল করতে দেয়। এইভাবে আপনার অ্যাপ থেকে একটি ফাংশন কল করতে, ক্লাউড ফাংশনে একটি HTTP কলযোগ্য ফাংশন লিখুন এবং স্থাপন করুন এবং তারপরে আপনার অ্যাপ থেকে ফাংশনটি কল করতে ক্লায়েন্ট লজিক যোগ করুন।

এটি মনে রাখা গুরুত্বপূর্ণ যে HTTP কলযোগ্য ফাংশনগুলি একই রকম তবে HTTP ফাংশনের সাথে অভিন্ন নয় ৷ HTTP কলযোগ্য ফাংশন ব্যবহার করার জন্য আপনাকে অবশ্যই ব্যাকএন্ড API (বা প্রোটোকল বাস্তবায়ন) সহ আপনার প্ল্যাটফর্মের জন্য ক্লায়েন্ট SDK ব্যবহার করতে হবে। কলেবলের HTTP ফাংশন থেকে এই মূল পার্থক্য রয়েছে:

  • কলেবলের সাথে, ফায়ারবেস প্রমাণীকরণ টোকেন, এফসিএম টোকেন এবং অ্যাপ চেক টোকেন, যখন উপলব্ধ, স্বয়ংক্রিয়ভাবে অনুরোধে অন্তর্ভুক্ত হয়।
  • ট্রিগার স্বয়ংক্রিয়ভাবে অনুরোধের মূল অংশটিকে ডিসিরিয়ালাইজ করে এবং প্রমাণীকরণ টোকেন যাচাই করে।

ক্লাউড ফাংশনগুলির জন্য Firebase SDK 2nd gen এবং উচ্চতর এই Firebase ক্লায়েন্ট SDK ন্যূনতম সংস্করণগুলির সাথে HTTPS কলযোগ্য ফাংশনগুলিকে সমর্থন করে:

  • Apple প্ল্যাটফর্মের জন্য Firebase SDK 10.19.0
  • Android 20.4.0 এর জন্য Firebase SDK
  • ফায়ারবেস মডুলার ওয়েব SDK v. 9.7.0

আপনি যদি একটি অসমর্থিত প্ল্যাটফর্মে নির্মিত একটি অ্যাপে অনুরূপ কার্যকারিতা যোগ করতে চান, তাহলে https.onCall এর জন্য প্রোটোকল স্পেসিফিকেশন দেখুন। এই গাইডের বাকি অংশে অ্যাপল প্ল্যাটফর্ম, অ্যান্ড্রয়েড, ওয়েব, সি++ এবং ইউনিটির জন্য একটি HTTP কলযোগ্য ফাংশন কীভাবে লিখতে, স্থাপন করতে এবং কল করতে হয় তার নির্দেশাবলী প্রদান করে।

কলযোগ্য ফাংশন লিখুন এবং স্থাপন করুন

একটি HTTPS কলযোগ্য ফাংশন তৈরি করতে functions.https.onCall ব্যবহার করুন। এই পদ্ধতিতে দুটি পরামিতি লাগে: data , এবং ঐচ্ছিক context :

// Saves a message to the Firebase Realtime Database but sanitizes the text by removing swearwords.
exports.addMessage = functions.https.onCall((data, context) => {
  // ...
});

একটি কলযোগ্য ফাংশনের জন্য যা রিয়েলটাইম ডেটাবেসে একটি পাঠ্য বার্তা সংরক্ষণ করে, উদাহরণস্বরূপ, data বার্তা পাঠ্য থাকতে পারে, যখন context প্যারামিটারগুলি ব্যবহারকারীর প্রমাণীকরণ তথ্য উপস্থাপন করে:

// Message text passed from the client.
const text = data.text;
// Authentication / user information is automatically added to the request.
const uid = context.auth.uid;
const name = context.auth.token.name || null;
const picture = context.auth.token.picture || null;
const email = context.auth.token.email || null;

কলযোগ্য ফাংশনের অবস্থান এবং কলিং ক্লায়েন্টের অবস্থানের মধ্যে দূরত্ব নেটওয়ার্ক লেটেন্সি তৈরি করতে পারে। কর্মক্ষমতা অপ্টিমাইজ করার জন্য, যেখানে প্রযোজ্য সেখানে ফাংশন অবস্থান নির্দিষ্ট করার কথা বিবেচনা করুন এবং আপনি যখন ক্লায়েন্ট সাইডে SDK শুরু করবেন তখন সেট করা লোকেশনের সাথে কলযোগ্য অবস্থানটি সারিবদ্ধ করা নিশ্চিত করুন৷

ঐচ্ছিকভাবে, আপনি বিলিং জালিয়াতি বা ফিশিংয়ের মতো অপব্যবহার থেকে আপনার ব্যাকএন্ড সংস্থানগুলিকে রক্ষা করতে সহায়তা করার জন্য একটি অ্যাপ চেক প্রত্যয়ন সংযুক্ত করতে পারেন। ক্লাউড ফাংশনের জন্য অ্যাপ চেক এনফোর্সমেন্ট সক্ষম করুন দেখুন।

ফলাফল ফেরত পাঠানো হচ্ছে

ক্লায়েন্টের কাছে ডেটা ফেরত পাঠাতে, JSON এনকোড করা যেতে পারে এমন ডেটা ফেরত দিন। উদাহরণস্বরূপ, একটি সংযোজন অপারেশনের ফলাফল ফেরত দিতে:

// returning result.
return {
  firstNumber: firstNumber,
  secondNumber: secondNumber,
  operator: '+',
  operationResult: firstNumber + secondNumber,
};

একটি অ্যাসিঙ্ক্রোনাস অপারেশনের পরে ডেটা ফেরত দিতে, একটি প্রতিশ্রুতি ফেরত দিন। প্রতিশ্রুতি দ্বারা ফিরে আসা ডেটা ক্লায়েন্টের কাছে ফেরত পাঠানো হয়। উদাহরণ স্বরূপ, আপনি স্যানিটাইজড টেক্সট ফেরত দিতে পারেন যা কলযোগ্য ফাংশন রিয়েলটাইম ডেটাবেসে লিখেছিল:

// Saving the new message to the Realtime Database.
const sanitizedMessage = sanitizer.sanitizeText(text); // Sanitize the message.
return admin.database().ref('/messages').push({
  text: sanitizedMessage,
  author: { uid, name, picture, email },
}).then(() => {
  console.log('New Message written');
  // Returning the sanitized message to the client.
  return { text: sanitizedMessage };
})

ত্রুটিগুলি পরিচালনা করুন

ক্লায়েন্ট কার্যকর ত্রুটির বিবরণ পেয়েছে তা নিশ্চিত করতে, ফাংশনের একটি উদাহরণ ছুঁড়ে (বা প্রত্যাখ্যান করা একটি প্রতিশ্রুতি ফেরত) দিয়ে কলযোগ্য থেকে ত্রুটিগুলি ফিরিয়ে দিন। functions.https.HttpsError । ত্রুটিটির একটি code বৈশিষ্ট্য রয়েছে যা functions.https.HttpsError এ তালিকাভুক্ত মানগুলির মধ্যে একটি হতে পারে। ত্রুটিগুলির একটি স্ট্রিং message রয়েছে, যা একটি খালি স্ট্রিং থেকে ডিফল্ট। তাদের একটি ইচ্ছাকৃত মান সহ একটি ঐচ্ছিক details ক্ষেত্র থাকতে পারে। যদি আপনার ফাংশন থেকে HttpsError ব্যতীত অন্য একটি ত্রুটি নিক্ষেপ করা হয়, তাহলে আপনার ক্লায়েন্ট পরিবর্তে বার্তা INTERNAL এবং কোড internal সহ একটি ত্রুটি পায়।

উদাহরণস্বরূপ, একটি ফাংশন কলিং ক্লায়েন্টে ফিরে আসার জন্য ত্রুটি বার্তা সহ ডেটা যাচাইকরণ এবং প্রমাণীকরণ ত্রুটি ফেলতে পারে:

// Checking attribute.
if (!(typeof text === 'string') || text.length === 0) {
  // Throwing an HttpsError so that the client gets the error details.
  throw new functions.https.HttpsError('invalid-argument', 'The function must be called with ' +
      'one arguments "text" containing the message text to add.');
}
// Checking that the user is authenticated.
if (!context.auth) {
  // Throwing an HttpsError so that the client gets the error details.
  throw new functions.https.HttpsError('failed-precondition', 'The function must be called ' +
      'while authenticated.');
}

কলযোগ্য ফাংশন স্থাপন করুন

আপনি index.js এর মধ্যে একটি সম্পূর্ণ কলযোগ্য ফাংশন সংরক্ষণ করার পরে, আপনি যখন firebase deploy চালান তখন অন্যান্য সমস্ত ফাংশনের সাথে এটি স্থাপন করা হয়। শুধুমাত্র কলযোগ্য স্থাপন করতে, আংশিক স্থাপনা সম্পাদন করতে দেখানো হিসাবে --only আর্গুমেন্ট ব্যবহার করুন:

firebase deploy --only functions:addMessage

ফাংশন স্থাপন করার সময় আপনি যদি অনুমতি ত্রুটির সম্মুখীন হন, তাহলে নিশ্চিত করুন যে যথাযথ IAM ভূমিকাগুলি স্থাপনার কমান্ড চালাচ্ছেন এমন ব্যবহারকারীকে বরাদ্দ করা হয়েছে।

আপনার ক্লায়েন্ট উন্নয়ন পরিবেশ সেট আপ করুন

নিশ্চিত করুন যে আপনি কোনো পূর্বশর্ত পূরণ করেছেন, তারপর আপনার অ্যাপে প্রয়োজনীয় নির্ভরতা এবং ক্লায়েন্ট লাইব্রেরি যোগ করুন।

iOS+

আপনার Apple অ্যাপে Firebase যোগ করতে নির্দেশাবলী অনুসরণ করুন।

ফায়ারবেস নির্ভরতা ইনস্টল এবং পরিচালনা করতে সুইফট প্যাকেজ ম্যানেজার ব্যবহার করুন।

  1. Xcode-এ, আপনার অ্যাপ প্রকল্প খোলার সাথে, ফাইল > প্যাকেজ যোগ করুন- এ নেভিগেট করুন।
  2. অনুরোধ করা হলে, Firebase Apple প্ল্যাটফর্ম SDK সংগ্রহস্থল যোগ করুন:
  3.   https://github.com/firebase/firebase-ios-sdk.git
  4. ক্লাউড ফাংশন লাইব্রেরি নির্বাচন করুন।
  5. আপনার লক্ষ্যের বিল্ড সেটিংসের অন্যান্য লিঙ্কার ফ্ল্যাগ বিভাগে -ObjC পতাকা যোগ করুন।
  6. শেষ হয়ে গেলে, Xcode স্বয়ংক্রিয়ভাবে পটভূমিতে আপনার নির্ভরতাগুলি সমাধান এবং ডাউনলোড করা শুরু করবে।

ওয়েব মডুলার API

  1. আপনার ওয়েব অ্যাপে Firebase যোগ করতে নির্দেশাবলী অনুসরণ করুন। আপনার টার্মিনাল থেকে নিম্নলিখিত কমান্ড চালানো নিশ্চিত করুন:
    npm install firebase@10.7.1 --save
    
  2. ম্যানুয়ালি ফায়ারবেস কোর এবং ক্লাউড ফাংশন উভয়ই প্রয়োজন:

     import { initializeApp } from 'firebase/app';
     import { getFunctions } from 'firebase/functions';
    
     const app = initializeApp({
         projectId: '### CLOUD FUNCTIONS PROJECT ID ###',
         apiKey: '### FIREBASE API KEY ###',
         authDomain: '### FIREBASE AUTH DOMAIN ###',
       });
     const functions = getFunctions(app);
    

ওয়েব নামস্থান API

  1. আপনার ওয়েব অ্যাপে Firebase যোগ করতে নির্দেশাবলী অনুসরণ করুন।
  2. আপনার অ্যাপে Firebase কোর এবং ক্লাউড ফাংশন ক্লায়েন্ট লাইব্রেরি যোগ করুন:
    <script src="https://www.gstatic.com/firebasejs/8.10.1/firebase.js"></script>
    <script src="https://www.gstatic.com/firebasejs/8.10.1/firebase-functions.js"></script>
    

ক্লাউড ফাংশন SDK একটি npm প্যাকেজ হিসাবে উপলব্ধ।

  1. আপনার টার্মিনাল থেকে নিম্নলিখিত কমান্ডটি চালান:
    npm install firebase@8.10.1 --save
    
  2. ম্যানুয়ালি ফায়ারবেস কোর এবং ক্লাউড ফাংশন উভয়ই প্রয়োজন:
    const firebase = require("firebase");
    // Required for side-effects
    require("firebase/functions");
    

Kotlin+KTX

  1. আপনার Android অ্যাপে Firebase যোগ করতে নির্দেশাবলী অনুসরণ করুন।

  2. আপনার মডিউলে (অ্যাপ-লেভেল) গ্রেডল ফাইলে (সাধারণত <project>/<app-module>/build.gradle.kts বা <project>/<app-module>/build.gradle ), ক্লাউড ফাংশনের জন্য নির্ভরতা যোগ করুন অ্যান্ড্রয়েডের জন্য লাইব্রেরি। আমরা লাইব্রেরি সংস্করণ নিয়ন্ত্রণ করতে Firebase Android BoM ব্যবহার করার পরামর্শ দিই।

    dependencies {
        // Import the BoM for the Firebase platform
        implementation(platform("com.google.firebase:firebase-bom:32.7.0"))
    
        // Add the dependency for the Cloud Functions library
        // When using the BoM, you don't specify versions in Firebase library dependencies
        implementation("com.google.firebase:firebase-functions")
    }
    

    Firebase Android BoM ব্যবহার করে, আপনার অ্যাপ সবসময় Firebase Android লাইব্রেরির সামঞ্জস্যপূর্ণ সংস্করণ ব্যবহার করবে।

    (বিকল্প) BoM ব্যবহার না করে Firebase লাইব্রেরি নির্ভরতা যোগ করুন

    আপনি যদি Firebase BoM ব্যবহার না করা বেছে নেন, তাহলে আপনাকে অবশ্যই প্রতিটি Firebase লাইব্রেরি সংস্করণ তার নির্ভরতা লাইনে উল্লেখ করতে হবে।

    মনে রাখবেন যে আপনি যদি আপনার অ্যাপে একাধিক ফায়ারবেস লাইব্রেরি ব্যবহার করেন, আমরা দৃঢ়ভাবে লাইব্রেরি সংস্করণগুলি পরিচালনা করতে BoM ব্যবহার করার পরামর্শ দিই, যা নিশ্চিত করে যে সমস্ত সংস্করণ সামঞ্জস্যপূর্ণ।

    dependencies {
        // Add the dependency for the Cloud Functions library
        // When NOT using the BoM, you must specify versions in Firebase library dependencies
        implementation("com.google.firebase:firebase-functions:20.4.0")
    }
    
    একটি কোটলিন-নির্দিষ্ট লাইব্রেরি মডিউল খুঁজছেন? অক্টোবর 2023 থেকে শুরু হচ্ছে (Firebase BoM 32.5.0) , Kotlin এবং Java ডেভেলপাররা প্রধান লাইব্রেরি মডিউলের উপর নির্ভর করতে পারে (বিস্তারিত জানতে, এই উদ্যোগ সম্পর্কে প্রায়শই জিজ্ঞাসিত প্রশ্ন দেখুন)।

Java

  1. আপনার Android অ্যাপে Firebase যোগ করতে নির্দেশাবলী অনুসরণ করুন।

  2. আপনার মডিউলে (অ্যাপ-লেভেল) গ্রেডল ফাইলে (সাধারণত <project>/<app-module>/build.gradle.kts বা <project>/<app-module>/build.gradle ), ক্লাউড ফাংশনের জন্য নির্ভরতা যোগ করুন অ্যান্ড্রয়েডের জন্য লাইব্রেরি। আমরা লাইব্রেরি সংস্করণ নিয়ন্ত্রণ করতে Firebase Android BoM ব্যবহার করার পরামর্শ দিই।

    dependencies {
        // Import the BoM for the Firebase platform
        implementation(platform("com.google.firebase:firebase-bom:32.7.0"))
    
        // Add the dependency for the Cloud Functions library
        // When using the BoM, you don't specify versions in Firebase library dependencies
        implementation("com.google.firebase:firebase-functions")
    }
    

    Firebase Android BoM ব্যবহার করে, আপনার অ্যাপ সবসময় Firebase Android লাইব্রেরির সামঞ্জস্যপূর্ণ সংস্করণ ব্যবহার করবে।

    (বিকল্প) BoM ব্যবহার না করে Firebase লাইব্রেরি নির্ভরতা যোগ করুন

    আপনি যদি Firebase BoM ব্যবহার না করা বেছে নেন, তাহলে আপনাকে অবশ্যই প্রতিটি Firebase লাইব্রেরি সংস্করণ তার নির্ভরতা লাইনে উল্লেখ করতে হবে।

    মনে রাখবেন যে আপনি যদি আপনার অ্যাপে একাধিক ফায়ারবেস লাইব্রেরি ব্যবহার করেন, আমরা দৃঢ়ভাবে লাইব্রেরি সংস্করণগুলি পরিচালনা করতে BoM ব্যবহার করার পরামর্শ দিই, যা নিশ্চিত করে যে সমস্ত সংস্করণ সামঞ্জস্যপূর্ণ।

    dependencies {
        // Add the dependency for the Cloud Functions library
        // When NOT using the BoM, you must specify versions in Firebase library dependencies
        implementation("com.google.firebase:firebase-functions:20.4.0")
    }
    
    একটি কোটলিন-নির্দিষ্ট লাইব্রেরি মডিউল খুঁজছেন? অক্টোবর 2023 থেকে শুরু হচ্ছে (Firebase BoM 32.5.0) , Kotlin এবং Java ডেভেলপাররা প্রধান লাইব্রেরি মডিউলের উপর নির্ভর করতে পারে (বিস্তারিত জানতে, এই উদ্যোগ সম্পর্কে প্রায়শই জিজ্ঞাসিত প্রশ্ন দেখুন)।

Dart

  1. আপনার Flutter অ্যাপে Firebase যোগ করতে নির্দেশাবলী অনুসরণ করুন।

  2. আপনার ফ্লাটার প্রকল্পের মূল থেকে, প্লাগইন ইনস্টল করতে নিম্নলিখিত কমান্ডটি চালান:

    flutter pub add cloud_functions
    
  3. একবার সম্পূর্ণ হয়ে গেলে, আপনার ফ্লাটার অ্যাপ্লিকেশন পুনর্নির্মাণ করুন:

    flutter run
    
  4. একবার ইনস্টল হয়ে গেলে, আপনি আপনার ডার্ট কোডে আমদানি করে cloud_functions প্লাগইন অ্যাক্সেস করতে পারেন:

    import 'package:cloud_functions/cloud_functions.dart';
    

সি++

Android এর সাথে C++ এর জন্য:

  1. আপনার C++ প্রকল্পে Firebase যোগ করতে নির্দেশাবলী অনুসরণ করুন।
  2. আপনার CMakeLists.txt ফাইলে firebase_functions লাইব্রেরি যোগ করুন।

অ্যাপল প্ল্যাটফর্মের সাথে C++ এর জন্য:

  1. আপনার C++ প্রকল্পে Firebase যোগ করতে নির্দেশাবলী অনুসরণ করুন।
  2. আপনার Podfile ক্লাউড ফাংশন পড যোগ করুন:
    pod 'Firebase/Functions'
  3. ফাইলটি সংরক্ষণ করুন, তারপর চালান:
    pod install
  4. আপনার Xcode প্রকল্পে Firebase C++ SDK থেকে Firebase কোর এবং ক্লাউড ফাংশন ফ্রেমওয়ার্ক যোগ করুন।
    • firebase.framework
    • firebase_functions.framework

ঐক্য

  1. আপনার ইউনিটি প্রকল্পে Firebase যোগ করতে নির্দেশাবলী অনুসরণ করুন।
  2. আপনার ইউনিটি প্রোজেক্টে Firebase Unity SDK থেকে FirebaseFunctions.unitypackage যোগ করুন।

ক্লায়েন্ট SDK শুরু করুন

ক্লাউড ফাংশনগুলির একটি উদাহরণ শুরু করুন:

সুইফট

lazy var functions = Functions.functions()

উদ্দেশ্য গ

@property(strong, nonatomic) FIRFunctions *functions;
// ...
self.functions = [FIRFunctions functions];

ওয়েব নামস্থান API

firebase.initializeApp({
  apiKey: '### FIREBASE API KEY ###',
  authDomain: '### FIREBASE AUTH DOMAIN ###',
  projectId: '### CLOUD FUNCTIONS PROJECT ID ###'
  databaseURL: 'https://### YOUR DATABASE NAME ###.firebaseio.com',
});

// Initialize Cloud Functions through Firebase
var functions = firebase.functions();

ওয়েব মডুলার API

const app = initializeApp({
  projectId: '### CLOUD FUNCTIONS PROJECT ID ###',
  apiKey: '### FIREBASE API KEY ###',
  authDomain: '### FIREBASE AUTH DOMAIN ###',
});
const functions = getFunctions(app);

Kotlin+KTX

private lateinit var functions: FirebaseFunctions
// ...
functions = Firebase.functions

Java

private FirebaseFunctions mFunctions;
// ...
mFunctions = FirebaseFunctions.getInstance();

Dart

final functions = FirebaseFunctions.instance;

সি++

firebase::functions::Functions* functions;
// ...
functions = firebase::functions::Functions::GetInstance(app);

ঐক্য

functions = Firebase.Functions.DefaultInstance;

ফাংশন কল

সুইফট

functions.httpsCallable("addMessage").call(["text": inputField.text]) { result, error in
  if let error = error as NSError? {
    if error.domain == FunctionsErrorDomain {
      let code = FunctionsErrorCode(rawValue: error.code)
      let message = error.localizedDescription
      let details = error.userInfo[FunctionsErrorDetailsKey]
    }
    // ...
  }
  if let data = result?.data as? [String: Any], let text = data["text"] as? String {
    self.resultField.text = text
  }
}

উদ্দেশ্য গ

[[_functions HTTPSCallableWithName:@"addMessage"] callWithObject:@{@"text": _inputField.text}
                                                      completion:^(FIRHTTPSCallableResult * _Nullable result, NSError * _Nullable error) {
  if (error) {
    if ([error.domain isEqual:@"com.firebase.functions"]) {
      FIRFunctionsErrorCode code = error.code;
      NSString *message = error.localizedDescription;
      NSObject *details = error.userInfo[@"details"];
    }
    // ...
  }
  self->_resultField.text = result.data[@"text"];
}];

ওয়েব নামস্থান API

var addMessage = firebase.functions().httpsCallable('addMessage');
addMessage({ text: messageText })
  .then((result) => {
    // Read result of the Cloud Function.
    var sanitizedMessage = result.data.text;
  });

ওয়েব মডুলার API

import { getFunctions, httpsCallable } from "firebase/functions";

const functions = getFunctions();
const addMessage = httpsCallable(functions, 'addMessage');
addMessage({ text: messageText })
  .then((result) => {
    // Read result of the Cloud Function.
    /** @type {any} */
    const data = result.data;
    const sanitizedMessage = data.text;
  });

Kotlin+KTX

private fun addMessage(text: String): Task<String> {
    // Create the arguments to the callable function.
    val data = hashMapOf(
        "text" to text,
        "push" to true,
    )

    return functions
        .getHttpsCallable("addMessage")
        .call(data)
        .continueWith { task ->
            // This continuation runs on either success or failure, but if the task
            // has failed then result will throw an Exception which will be
            // propagated down.
            val result = task.result?.data as String
            result
        }
}

Java

private Task<String> addMessage(String text) {
    // Create the arguments to the callable function.
    Map<String, Object> data = new HashMap<>();
    data.put("text", text);
    data.put("push", true);

    return mFunctions
            .getHttpsCallable("addMessage")
            .call(data)
            .continueWith(new Continuation<HttpsCallableResult, String>() {
                @Override
                public String then(@NonNull Task<HttpsCallableResult> task) throws Exception {
                    // This continuation runs on either success or failure, but if the task
                    // has failed then getResult() will throw an Exception which will be
                    // propagated down.
                    String result = (String) task.getResult().getData();
                    return result;
                }
            });
}

Dart

    final result = await FirebaseFunctions.instance.httpsCallable('addMessage').call(
      {
        "text": text,
        "push": true,
      },
    );
    _response = result.data as String;

সি++

firebase::Future<firebase::functions::HttpsCallableResult> AddMessage(
    const std::string& text) {
  // Create the arguments to the callable function.
  firebase::Variant data = firebase::Variant::EmptyMap();
  data.map()["text"] = firebase::Variant(text);
  data.map()["push"] = true;

  // Call the function and add a callback for the result.
  firebase::functions::HttpsCallableReference doSomething =
      functions->GetHttpsCallable("addMessage");
  return doSomething.Call(data);
}

ঐক্য

private Task<string> addMessage(string text) {
  // Create the arguments to the callable function.
  var data = new Dictionary<string, object>();
  data["text"] = text;
  data["push"] = true;

  // Call the function and extract the operation from the result.
  var function = functions.GetHttpsCallable("addMessage");
  return function.CallAsync(data).ContinueWith((task) => {
    return (string) task.Result.Data;
  });
}

ক্লায়েন্টে ত্রুটিগুলি পরিচালনা করুন

ক্লায়েন্ট একটি ত্রুটি পায় যদি সার্ভার একটি ত্রুটি নিক্ষেপ করে বা ফলাফল প্রতিশ্রুতি প্রত্যাখ্যান করা হয়।

যদি ফাংশন দ্বারা প্রত্যাবর্তিত ত্রুটিটি function.https.HttpsError টাইপের হয়, তাহলে ক্লায়েন্ট সার্ভার ত্রুটি থেকে ত্রুটি code , message এবং details গ্রহণ করে। অন্যথায়, ত্রুটিটিতে বার্তাটি INTERNAL এবং কোডটি INTERNAL রয়েছে। আপনার কলযোগ্য ফাংশনে ত্রুটিগুলি কীভাবে পরিচালনা করবেন তার নির্দেশিকা দেখুন।

সুইফট

if let error = error as NSError? {
  if error.domain == FunctionsErrorDomain {
    let code = FunctionsErrorCode(rawValue: error.code)
    let message = error.localizedDescription
    let details = error.userInfo[FunctionsErrorDetailsKey]
  }
  // ...
}

উদ্দেশ্য গ

if (error) {
  if ([error.domain isEqual:@"com.firebase.functions"]) {
    FIRFunctionsErrorCode code = error.code;
    NSString *message = error.localizedDescription;
    NSObject *details = error.userInfo[@"details"];
  }
  // ...
}

ওয়েব নামস্থান API

var addMessage = firebase.functions().httpsCallable('addMessage');
addMessage({ text: messageText })
  .then((result) => {
    // Read result of the Cloud Function.
    var sanitizedMessage = result.data.text;
  })
  .catch((error) => {
    // Getting the Error details.
    var code = error.code;
    var message = error.message;
    var details = error.details;
    // ...
  });

ওয়েব মডুলার API

import { getFunctions, httpsCallable } from "firebase/functions";

const functions = getFunctions();
const addMessage = httpsCallable(functions, 'addMessage');
addMessage({ text: messageText })
  .then((result) => {
    // Read result of the Cloud Function.
    /** @type {any} */
    const data = result.data;
    const sanitizedMessage = data.text;
  })
  .catch((error) => {
    // Getting the Error details.
    const code = error.code;
    const message = error.message;
    const details = error.details;
    // ...
  });

Kotlin+KTX

addMessage(inputMessage)
    .addOnCompleteListener { task ->
        if (!task.isSuccessful) {
            val e = task.exception
            if (e is FirebaseFunctionsException) {
                val code = e.code
                val details = e.details
            }
        }
    }

Java

addMessage(inputMessage)
        .addOnCompleteListener(new OnCompleteListener<String>() {
            @Override
            public void onComplete(@NonNull Task<String> task) {
                if (!task.isSuccessful()) {
                    Exception e = task.getException();
                    if (e instanceof FirebaseFunctionsException) {
                        FirebaseFunctionsException ffe = (FirebaseFunctionsException) e;
                        FirebaseFunctionsException.Code code = ffe.getCode();
                        Object details = ffe.getDetails();
                    }
                }
            }
        });

Dart

try {
  final result =
      await FirebaseFunctions.instance.httpsCallable('addMessage').call();
} on FirebaseFunctionsException catch (error) {
  print(error.code);
  print(error.details);
  print(error.message);
}

সি++

void OnAddMessageCallback(
    const firebase::Future<firebase::functions::HttpsCallableResult>& future) {
  if (future.error() != firebase::functions::kErrorNone) {
    // Function error code, will be kErrorInternal if the failure was not
    // handled properly in the function call.
    auto code = static_cast<firebase::functions::Error>(future.error());

    // Display the error in the UI.
    DisplayError(code, future.error_message());
    return;
  }

  const firebase::functions::HttpsCallableResult* result = future.result();
  firebase::Variant data = result->data();
  // This will assert if the result returned from the function wasn't a string.
  std::string message = data.string_value();
  // Display the result in the UI.
  DisplayResult(message);
}

// ...

// ...
  auto future = AddMessage(message);
  future.OnCompletion(OnAddMessageCallback);
  // ...

ঐক্য

 addMessage(text).ContinueWith((task) => {
  if (task.IsFaulted) {
    foreach (var inner in task.Exception.InnerExceptions) {
      if (inner is FunctionsException) {
        var e = (FunctionsException) inner;
        // Function error code, will be INTERNAL if the failure
        // was not handled properly in the function call.
        var code = e.ErrorCode;
        var message = e.ErrorMessage;
      }
    }
  } else {
    string result = task.Result;
  }
});

আপনি আপনার অ্যাপ চালু করার আগে, শুধুমাত্র আপনার অ্যাপগুলি আপনার কলযোগ্য ফাংশন এন্ডপয়েন্টগুলি অ্যাক্সেস করতে পারে তা নিশ্চিত করতে সহায়তা করার জন্য আপনাকে অ্যাপ চেক সক্ষম করতে হবে।

,


Firebase ক্লায়েন্ট SDK-এর জন্য ক্লাউড ফাংশন আপনাকে Firebase অ্যাপ থেকে সরাসরি ফাংশন কল করতে দেয়। এইভাবে আপনার অ্যাপ থেকে একটি ফাংশন কল করতে, ক্লাউড ফাংশনে একটি HTTP কলযোগ্য ফাংশন লিখুন এবং স্থাপন করুন এবং তারপরে আপনার অ্যাপ থেকে ফাংশনটি কল করতে ক্লায়েন্ট লজিক যোগ করুন।

এটি মনে রাখা গুরুত্বপূর্ণ যে HTTP কলযোগ্য ফাংশনগুলি একই রকম তবে HTTP ফাংশনের সাথে অভিন্ন নয় ৷ HTTP কলযোগ্য ফাংশন ব্যবহার করার জন্য আপনাকে অবশ্যই ব্যাকএন্ড API (বা প্রোটোকল বাস্তবায়ন) সহ আপনার প্ল্যাটফর্মের জন্য ক্লায়েন্ট SDK ব্যবহার করতে হবে। কলেবলের HTTP ফাংশন থেকে এই মূল পার্থক্য রয়েছে:

  • কলেবলের সাথে, ফায়ারবেস প্রমাণীকরণ টোকেন, এফসিএম টোকেন এবং অ্যাপ চেক টোকেন, যখন উপলব্ধ, স্বয়ংক্রিয়ভাবে অনুরোধে অন্তর্ভুক্ত হয়।
  • ট্রিগার স্বয়ংক্রিয়ভাবে অনুরোধের মূল অংশটিকে ডিসিরিয়ালাইজ করে এবং প্রমাণীকরণ টোকেন যাচাই করে।

ক্লাউড ফাংশনগুলির জন্য Firebase SDK 2nd gen এবং উচ্চতর এই Firebase ক্লায়েন্ট SDK ন্যূনতম সংস্করণগুলির সাথে HTTPS কলযোগ্য ফাংশনগুলিকে সমর্থন করে:

  • Apple প্ল্যাটফর্মের জন্য Firebase SDK 10.19.0
  • Android 20.4.0 এর জন্য Firebase SDK
  • ফায়ারবেস মডুলার ওয়েব SDK v. 9.7.0

আপনি যদি একটি অসমর্থিত প্ল্যাটফর্মে নির্মিত একটি অ্যাপে অনুরূপ কার্যকারিতা যোগ করতে চান, তাহলে https.onCall এর জন্য প্রোটোকল স্পেসিফিকেশন দেখুন। এই গাইডের বাকি অংশে অ্যাপল প্ল্যাটফর্ম, অ্যান্ড্রয়েড, ওয়েব, সি++ এবং ইউনিটির জন্য একটি HTTP কলযোগ্য ফাংশন কীভাবে লিখতে, স্থাপন করতে এবং কল করতে হয় তার নির্দেশাবলী প্রদান করে।

কলযোগ্য ফাংশন লিখুন এবং স্থাপন করুন

একটি HTTPS কলযোগ্য ফাংশন তৈরি করতে functions.https.onCall ব্যবহার করুন। এই পদ্ধতিতে দুটি পরামিতি লাগে: data , এবং ঐচ্ছিক context :

// Saves a message to the Firebase Realtime Database but sanitizes the text by removing swearwords.
exports.addMessage = functions.https.onCall((data, context) => {
  // ...
});

একটি কলযোগ্য ফাংশনের জন্য যা রিয়েলটাইম ডেটাবেসে একটি পাঠ্য বার্তা সংরক্ষণ করে, উদাহরণস্বরূপ, data বার্তা পাঠ্য থাকতে পারে, যখন context প্যারামিটারগুলি ব্যবহারকারীর প্রমাণীকরণ তথ্য উপস্থাপন করে:

// Message text passed from the client.
const text = data.text;
// Authentication / user information is automatically added to the request.
const uid = context.auth.uid;
const name = context.auth.token.name || null;
const picture = context.auth.token.picture || null;
const email = context.auth.token.email || null;

কলযোগ্য ফাংশনের অবস্থান এবং কলিং ক্লায়েন্টের অবস্থানের মধ্যে দূরত্ব নেটওয়ার্ক লেটেন্সি তৈরি করতে পারে। কর্মক্ষমতা অপ্টিমাইজ করার জন্য, যেখানে প্রযোজ্য সেখানে ফাংশন অবস্থান নির্দিষ্ট করার কথা বিবেচনা করুন এবং আপনি যখন ক্লায়েন্ট সাইডে SDK শুরু করবেন তখন সেট করা লোকেশনের সাথে কলযোগ্য অবস্থানটি সারিবদ্ধ করা নিশ্চিত করুন৷

ঐচ্ছিকভাবে, আপনি বিলিং জালিয়াতি বা ফিশিংয়ের মতো অপব্যবহার থেকে আপনার ব্যাকএন্ড সংস্থানগুলিকে রক্ষা করতে সহায়তা করার জন্য একটি অ্যাপ চেক প্রত্যয়ন সংযুক্ত করতে পারেন। ক্লাউড ফাংশনের জন্য অ্যাপ চেক এনফোর্সমেন্ট সক্ষম করুন দেখুন।

ফলাফল ফেরত পাঠানো হচ্ছে

ক্লায়েন্টের কাছে ডেটা ফেরত পাঠাতে, JSON এনকোড করা যেতে পারে এমন ডেটা ফেরত দিন। উদাহরণস্বরূপ, একটি সংযোজন অপারেশনের ফলাফল ফেরত দিতে:

// returning result.
return {
  firstNumber: firstNumber,
  secondNumber: secondNumber,
  operator: '+',
  operationResult: firstNumber + secondNumber,
};

একটি অ্যাসিঙ্ক্রোনাস অপারেশনের পরে ডেটা ফেরত দিতে, একটি প্রতিশ্রুতি ফেরত দিন। প্রতিশ্রুতি দ্বারা ফিরে আসা ডেটা ক্লায়েন্টের কাছে ফেরত পাঠানো হয়। উদাহরণ স্বরূপ, আপনি স্যানিটাইজড টেক্সট ফেরত দিতে পারেন যা কলযোগ্য ফাংশন রিয়েলটাইম ডেটাবেসে লিখেছিল:

// Saving the new message to the Realtime Database.
const sanitizedMessage = sanitizer.sanitizeText(text); // Sanitize the message.
return admin.database().ref('/messages').push({
  text: sanitizedMessage,
  author: { uid, name, picture, email },
}).then(() => {
  console.log('New Message written');
  // Returning the sanitized message to the client.
  return { text: sanitizedMessage };
})

ত্রুটিগুলি পরিচালনা করুন

ক্লায়েন্ট কার্যকর ত্রুটির বিবরণ পেয়েছে তা নিশ্চিত করতে, ফাংশনের একটি উদাহরণ ছুঁড়ে (বা প্রত্যাখ্যান করা একটি প্রতিশ্রুতি ফেরত) দিয়ে কলযোগ্য থেকে ত্রুটিগুলি ফিরিয়ে দিন। functions.https.HttpsError । ত্রুটিটির একটি code বৈশিষ্ট্য রয়েছে যা functions.https.HttpsError এ তালিকাভুক্ত মানগুলির মধ্যে একটি হতে পারে। ত্রুটিগুলির একটি স্ট্রিং message রয়েছে, যা একটি খালি স্ট্রিং থেকে ডিফল্ট। তাদের একটি ইচ্ছাকৃত মান সহ একটি ঐচ্ছিক details ক্ষেত্র থাকতে পারে। যদি আপনার ফাংশন থেকে HttpsError ব্যতীত অন্য একটি ত্রুটি নিক্ষেপ করা হয়, তাহলে আপনার ক্লায়েন্ট পরিবর্তে বার্তা INTERNAL এবং কোড internal সহ একটি ত্রুটি পায়।

উদাহরণস্বরূপ, একটি ফাংশন কলিং ক্লায়েন্টে ফিরে আসার জন্য ত্রুটি বার্তা সহ ডেটা যাচাইকরণ এবং প্রমাণীকরণ ত্রুটি ফেলতে পারে:

// Checking attribute.
if (!(typeof text === 'string') || text.length === 0) {
  // Throwing an HttpsError so that the client gets the error details.
  throw new functions.https.HttpsError('invalid-argument', 'The function must be called with ' +
      'one arguments "text" containing the message text to add.');
}
// Checking that the user is authenticated.
if (!context.auth) {
  // Throwing an HttpsError so that the client gets the error details.
  throw new functions.https.HttpsError('failed-precondition', 'The function must be called ' +
      'while authenticated.');
}

কলযোগ্য ফাংশন স্থাপন করুন

আপনি index.js এর মধ্যে একটি সম্পূর্ণ কলযোগ্য ফাংশন সংরক্ষণ করার পরে, আপনি যখন firebase deploy চালান তখন অন্যান্য সমস্ত ফাংশনের সাথে এটি স্থাপন করা হয়। শুধুমাত্র কলযোগ্য স্থাপন করতে, আংশিক স্থাপনা সম্পাদন করতে দেখানো হিসাবে --only আর্গুমেন্ট ব্যবহার করুন:

firebase deploy --only functions:addMessage

ফাংশন স্থাপন করার সময় আপনি যদি অনুমতি ত্রুটির সম্মুখীন হন, তাহলে নিশ্চিত করুন যে যথাযথ IAM ভূমিকাগুলি স্থাপনার কমান্ড চালাচ্ছেন এমন ব্যবহারকারীকে বরাদ্দ করা হয়েছে।

আপনার ক্লায়েন্ট উন্নয়ন পরিবেশ সেট আপ করুন

নিশ্চিত করুন যে আপনি কোনো পূর্বশর্ত পূরণ করেছেন, তারপর আপনার অ্যাপে প্রয়োজনীয় নির্ভরতা এবং ক্লায়েন্ট লাইব্রেরি যোগ করুন।

iOS+

আপনার Apple অ্যাপে Firebase যোগ করতে নির্দেশাবলী অনুসরণ করুন।

ফায়ারবেস নির্ভরতা ইনস্টল এবং পরিচালনা করতে সুইফট প্যাকেজ ম্যানেজার ব্যবহার করুন।

  1. Xcode-এ, আপনার অ্যাপ প্রকল্প খোলার সাথে, ফাইল > প্যাকেজ যোগ করুন- এ নেভিগেট করুন।
  2. অনুরোধ করা হলে, Firebase Apple প্ল্যাটফর্ম SDK সংগ্রহস্থল যোগ করুন:
  3.   https://github.com/firebase/firebase-ios-sdk.git
  4. ক্লাউড ফাংশন লাইব্রেরি নির্বাচন করুন।
  5. আপনার লক্ষ্যের বিল্ড সেটিংসের অন্যান্য লিঙ্কার ফ্ল্যাগ বিভাগে -ObjC পতাকা যোগ করুন।
  6. শেষ হয়ে গেলে, Xcode স্বয়ংক্রিয়ভাবে পটভূমিতে আপনার নির্ভরতাগুলি সমাধান এবং ডাউনলোড করা শুরু করবে।

ওয়েব মডুলার API

  1. আপনার ওয়েব অ্যাপে Firebase যোগ করতে নির্দেশাবলী অনুসরণ করুন। আপনার টার্মিনাল থেকে নিম্নলিখিত কমান্ড চালানো নিশ্চিত করুন:
    npm install firebase@10.7.1 --save
    
  2. ম্যানুয়ালি ফায়ারবেস কোর এবং ক্লাউড ফাংশন উভয়ই প্রয়োজন:

     import { initializeApp } from 'firebase/app';
     import { getFunctions } from 'firebase/functions';
    
     const app = initializeApp({
         projectId: '### CLOUD FUNCTIONS PROJECT ID ###',
         apiKey: '### FIREBASE API KEY ###',
         authDomain: '### FIREBASE AUTH DOMAIN ###',
       });
     const functions = getFunctions(app);
    

ওয়েব নামস্থান API

  1. আপনার ওয়েব অ্যাপে Firebase যোগ করতে নির্দেশাবলী অনুসরণ করুন।
  2. আপনার অ্যাপে Firebase কোর এবং ক্লাউড ফাংশন ক্লায়েন্ট লাইব্রেরি যোগ করুন:
    <script src="https://www.gstatic.com/firebasejs/8.10.1/firebase.js"></script>
    <script src="https://www.gstatic.com/firebasejs/8.10.1/firebase-functions.js"></script>
    

ক্লাউড ফাংশন SDK একটি npm প্যাকেজ হিসাবে উপলব্ধ।

  1. আপনার টার্মিনাল থেকে নিম্নলিখিত কমান্ডটি চালান:
    npm install firebase@8.10.1 --save
    
  2. ম্যানুয়ালি ফায়ারবেস কোর এবং ক্লাউড ফাংশন উভয়ই প্রয়োজন:
    const firebase = require("firebase");
    // Required for side-effects
    require("firebase/functions");
    

Kotlin+KTX

  1. আপনার Android অ্যাপে Firebase যোগ করতে নির্দেশাবলী অনুসরণ করুন।

  2. আপনার মডিউলে (অ্যাপ-লেভেল) গ্রেডল ফাইলে (সাধারণত <project>/<app-module>/build.gradle.kts বা <project>/<app-module>/build.gradle ), ক্লাউড ফাংশনের জন্য নির্ভরতা যোগ করুন অ্যান্ড্রয়েডের জন্য লাইব্রেরি। আমরা লাইব্রেরি সংস্করণ নিয়ন্ত্রণ করতে Firebase Android BoM ব্যবহার করার পরামর্শ দিই।

    dependencies {
        // Import the BoM for the Firebase platform
        implementation(platform("com.google.firebase:firebase-bom:32.7.0"))
    
        // Add the dependency for the Cloud Functions library
        // When using the BoM, you don't specify versions in Firebase library dependencies
        implementation("com.google.firebase:firebase-functions")
    }
    

    Firebase Android BoM ব্যবহার করে, আপনার অ্যাপ সবসময় Firebase Android লাইব্রেরির সামঞ্জস্যপূর্ণ সংস্করণ ব্যবহার করবে।

    (বিকল্প) BoM ব্যবহার না করে Firebase লাইব্রেরি নির্ভরতা যোগ করুন

    আপনি যদি Firebase BoM ব্যবহার না করা বেছে নেন, তাহলে আপনাকে অবশ্যই প্রতিটি Firebase লাইব্রেরি সংস্করণ তার নির্ভরতা লাইনে উল্লেখ করতে হবে।

    মনে রাখবেন যে আপনি যদি আপনার অ্যাপে একাধিক ফায়ারবেস লাইব্রেরি ব্যবহার করেন, আমরা দৃঢ়ভাবে লাইব্রেরি সংস্করণগুলি পরিচালনা করতে BoM ব্যবহার করার পরামর্শ দিই, যা নিশ্চিত করে যে সমস্ত সংস্করণ সামঞ্জস্যপূর্ণ।

    dependencies {
        // Add the dependency for the Cloud Functions library
        // When NOT using the BoM, you must specify versions in Firebase library dependencies
        implementation("com.google.firebase:firebase-functions:20.4.0")
    }
    
    একটি কোটলিন-নির্দিষ্ট লাইব্রেরি মডিউল খুঁজছেন? অক্টোবর 2023 থেকে শুরু হচ্ছে (Firebase BoM 32.5.0) , Kotlin এবং Java ডেভেলপাররা প্রধান লাইব্রেরি মডিউলের উপর নির্ভর করতে পারে (বিস্তারিত জানতে, এই উদ্যোগ সম্পর্কে প্রায়শই জিজ্ঞাসিত প্রশ্ন দেখুন)।

Java

  1. আপনার Android অ্যাপে Firebase যোগ করতে নির্দেশাবলী অনুসরণ করুন।

  2. আপনার মডিউলে (অ্যাপ-লেভেল) গ্রেডল ফাইলে (সাধারণত <project>/<app-module>/build.gradle.kts বা <project>/<app-module>/build.gradle ), ক্লাউড ফাংশনের জন্য নির্ভরতা যোগ করুন অ্যান্ড্রয়েডের জন্য লাইব্রেরি। আমরা লাইব্রেরি সংস্করণ নিয়ন্ত্রণ করতে Firebase Android BoM ব্যবহার করার পরামর্শ দিই।

    dependencies {
        // Import the BoM for the Firebase platform
        implementation(platform("com.google.firebase:firebase-bom:32.7.0"))
    
        // Add the dependency for the Cloud Functions library
        // When using the BoM, you don't specify versions in Firebase library dependencies
        implementation("com.google.firebase:firebase-functions")
    }
    

    Firebase Android BoM ব্যবহার করে, আপনার অ্যাপ সবসময় Firebase Android লাইব্রেরির সামঞ্জস্যপূর্ণ সংস্করণ ব্যবহার করবে।

    (বিকল্প) BoM ব্যবহার না করে Firebase লাইব্রেরি নির্ভরতা যোগ করুন

    আপনি যদি Firebase BoM ব্যবহার না করা বেছে নেন, তাহলে আপনাকে অবশ্যই প্রতিটি Firebase লাইব্রেরি সংস্করণ তার নির্ভরতা লাইনে উল্লেখ করতে হবে।

    মনে রাখবেন যে আপনি যদি আপনার অ্যাপে একাধিক ফায়ারবেস লাইব্রেরি ব্যবহার করেন, আমরা দৃঢ়ভাবে লাইব্রেরি সংস্করণগুলি পরিচালনা করতে BoM ব্যবহার করার পরামর্শ দিই, যা নিশ্চিত করে যে সমস্ত সংস্করণ সামঞ্জস্যপূর্ণ।

    dependencies {
        // Add the dependency for the Cloud Functions library
        // When NOT using the BoM, you must specify versions in Firebase library dependencies
        implementation("com.google.firebase:firebase-functions:20.4.0")
    }
    
    একটি কোটলিন-নির্দিষ্ট লাইব্রেরি মডিউল খুঁজছেন? অক্টোবর 2023 থেকে শুরু হচ্ছে (Firebase BoM 32.5.0) , Kotlin এবং Java ডেভেলপাররা প্রধান লাইব্রেরি মডিউলের উপর নির্ভর করতে পারে (বিস্তারিত জানতে, এই উদ্যোগ সম্পর্কে প্রায়শই জিজ্ঞাসিত প্রশ্ন দেখুন)।

Dart

  1. আপনার Flutter অ্যাপে Firebase যোগ করতে নির্দেশাবলী অনুসরণ করুন।

  2. আপনার ফ্লাটার প্রকল্পের মূল থেকে, প্লাগইন ইনস্টল করতে নিম্নলিখিত কমান্ডটি চালান:

    flutter pub add cloud_functions
    
  3. একবার সম্পূর্ণ হয়ে গেলে, আপনার ফ্লাটার অ্যাপ্লিকেশন পুনর্নির্মাণ করুন:

    flutter run
    
  4. একবার ইনস্টল হয়ে গেলে, আপনি আপনার ডার্ট কোডে আমদানি করে cloud_functions প্লাগইন অ্যাক্সেস করতে পারেন:

    import 'package:cloud_functions/cloud_functions.dart';
    

সি++

Android এর সাথে C++ এর জন্য:

  1. আপনার C++ প্রকল্পে Firebase যোগ করতে নির্দেশাবলী অনুসরণ করুন।
  2. আপনার CMakeLists.txt ফাইলে firebase_functions লাইব্রেরি যোগ করুন।

অ্যাপল প্ল্যাটফর্মের সাথে C++ এর জন্য:

  1. আপনার C++ প্রকল্পে Firebase যোগ করতে নির্দেশাবলী অনুসরণ করুন।
  2. আপনার Podfile ক্লাউড ফাংশন পড যোগ করুন:
    pod 'Firebase/Functions'
  3. ফাইলটি সংরক্ষণ করুন, তারপর চালান:
    pod install
  4. আপনার Xcode প্রকল্পে Firebase C++ SDK থেকে Firebase কোর এবং ক্লাউড ফাংশন ফ্রেমওয়ার্ক যোগ করুন।
    • firebase.framework
    • firebase_functions.framework

ঐক্য

  1. আপনার ইউনিটি প্রকল্পে Firebase যোগ করতে নির্দেশাবলী অনুসরণ করুন।
  2. আপনার ইউনিটি প্রোজেক্টে Firebase Unity SDK থেকে FirebaseFunctions.unitypackage যোগ করুন।

ক্লায়েন্ট SDK শুরু করুন

ক্লাউড ফাংশনগুলির একটি উদাহরণ শুরু করুন:

সুইফট

lazy var functions = Functions.functions()

উদ্দেশ্য গ

@property(strong, nonatomic) FIRFunctions *functions;
// ...
self.functions = [FIRFunctions functions];

ওয়েব নামস্থান API

firebase.initializeApp({
  apiKey: '### FIREBASE API KEY ###',
  authDomain: '### FIREBASE AUTH DOMAIN ###',
  projectId: '### CLOUD FUNCTIONS PROJECT ID ###'
  databaseURL: 'https://### YOUR DATABASE NAME ###.firebaseio.com',
});

// Initialize Cloud Functions through Firebase
var functions = firebase.functions();

ওয়েব মডুলার API

const app = initializeApp({
  projectId: '### CLOUD FUNCTIONS PROJECT ID ###',
  apiKey: '### FIREBASE API KEY ###',
  authDomain: '### FIREBASE AUTH DOMAIN ###',
});
const functions = getFunctions(app);

Kotlin+KTX

private lateinit var functions: FirebaseFunctions
// ...
functions = Firebase.functions

Java

private FirebaseFunctions mFunctions;
// ...
mFunctions = FirebaseFunctions.getInstance();

Dart

final functions = FirebaseFunctions.instance;

সি++

firebase::functions::Functions* functions;
// ...
functions = firebase::functions::Functions::GetInstance(app);

ঐক্য

functions = Firebase.Functions.DefaultInstance;

ফাংশন কল

সুইফট

functions.httpsCallable("addMessage").call(["text": inputField.text]) { result, error in
  if let error = error as NSError? {
    if error.domain == FunctionsErrorDomain {
      let code = FunctionsErrorCode(rawValue: error.code)
      let message = error.localizedDescription
      let details = error.userInfo[FunctionsErrorDetailsKey]
    }
    // ...
  }
  if let data = result?.data as? [String: Any], let text = data["text"] as? String {
    self.resultField.text = text
  }
}

উদ্দেশ্য গ

[[_functions HTTPSCallableWithName:@"addMessage"] callWithObject:@{@"text": _inputField.text}
                                                      completion:^(FIRHTTPSCallableResult * _Nullable result, NSError * _Nullable error) {
  if (error) {
    if ([error.domain isEqual:@"com.firebase.functions"]) {
      FIRFunctionsErrorCode code = error.code;
      NSString *message = error.localizedDescription;
      NSObject *details = error.userInfo[@"details"];
    }
    // ...
  }
  self->_resultField.text = result.data[@"text"];
}];

ওয়েব নামস্থান API

var addMessage = firebase.functions().httpsCallable('addMessage');
addMessage({ text: messageText })
  .then((result) => {
    // Read result of the Cloud Function.
    var sanitizedMessage = result.data.text;
  });

ওয়েব মডুলার API

import { getFunctions, httpsCallable } from "firebase/functions";

const functions = getFunctions();
const addMessage = httpsCallable(functions, 'addMessage');
addMessage({ text: messageText })
  .then((result) => {
    // Read result of the Cloud Function.
    /** @type {any} */
    const data = result.data;
    const sanitizedMessage = data.text;
  });

Kotlin+KTX

private fun addMessage(text: String): Task<String> {
    // Create the arguments to the callable function.
    val data = hashMapOf(
        "text" to text,
        "push" to true,
    )

    return functions
        .getHttpsCallable("addMessage")
        .call(data)
        .continueWith { task ->
            // This continuation runs on either success or failure, but if the task
            // has failed then result will throw an Exception which will be
            // propagated down.
            val result = task.result?.data as String
            result
        }
}

Java

private Task<String> addMessage(String text) {
    // Create the arguments to the callable function.
    Map<String, Object> data = new HashMap<>();
    data.put("text", text);
    data.put("push", true);

    return mFunctions
            .getHttpsCallable("addMessage")
            .call(data)
            .continueWith(new Continuation<HttpsCallableResult, String>() {
                @Override
                public String then(@NonNull Task<HttpsCallableResult> task) throws Exception {
                    // This continuation runs on either success or failure, but if the task
                    // has failed then getResult() will throw an Exception which will be
                    // propagated down.
                    String result = (String) task.getResult().getData();
                    return result;
                }
            });
}

Dart

    final result = await FirebaseFunctions.instance.httpsCallable('addMessage').call(
      {
        "text": text,
        "push": true,
      },
    );
    _response = result.data as String;

সি++

firebase::Future<firebase::functions::HttpsCallableResult> AddMessage(
    const std::string& text) {
  // Create the arguments to the callable function.
  firebase::Variant data = firebase::Variant::EmptyMap();
  data.map()["text"] = firebase::Variant(text);
  data.map()["push"] = true;

  // Call the function and add a callback for the result.
  firebase::functions::HttpsCallableReference doSomething =
      functions->GetHttpsCallable("addMessage");
  return doSomething.Call(data);
}

ঐক্য

private Task<string> addMessage(string text) {
  // Create the arguments to the callable function.
  var data = new Dictionary<string, object>();
  data["text"] = text;
  data["push"] = true;

  // Call the function and extract the operation from the result.
  var function = functions.GetHttpsCallable("addMessage");
  return function.CallAsync(data).ContinueWith((task) => {
    return (string) task.Result.Data;
  });
}

ক্লায়েন্টে ত্রুটিগুলি পরিচালনা করুন

ক্লায়েন্ট একটি ত্রুটি পায় যদি সার্ভার একটি ত্রুটি নিক্ষেপ করে বা ফলাফল প্রতিশ্রুতি প্রত্যাখ্যান করা হয়।

যদি ফাংশন দ্বারা প্রত্যাবর্তিত ত্রুটিটি function.https.HttpsError টাইপের হয়, তাহলে ক্লায়েন্ট সার্ভার ত্রুটি থেকে ত্রুটি code , message এবং details গ্রহণ করে। অন্যথায়, ত্রুটিটিতে বার্তাটি INTERNAL এবং কোডটি INTERNAL রয়েছে। আপনার কলযোগ্য ফাংশনে ত্রুটিগুলি কীভাবে পরিচালনা করবেন তার নির্দেশিকা দেখুন।

সুইফট

if let error = error as NSError? {
  if error.domain == FunctionsErrorDomain {
    let code = FunctionsErrorCode(rawValue: error.code)
    let message = error.localizedDescription
    let details = error.userInfo[FunctionsErrorDetailsKey]
  }
  // ...
}

উদ্দেশ্য গ

if (error) {
  if ([error.domain isEqual:@"com.firebase.functions"]) {
    FIRFunctionsErrorCode code = error.code;
    NSString *message = error.localizedDescription;
    NSObject *details = error.userInfo[@"details"];
  }
  // ...
}

ওয়েব নামস্থান API

var addMessage = firebase.functions().httpsCallable('addMessage');
addMessage({ text: messageText })
  .then((result) => {
    // Read result of the Cloud Function.
    var sanitizedMessage = result.data.text;
  })
  .catch((error) => {
    // Getting the Error details.
    var code = error.code;
    var message = error.message;
    var details = error.details;
    // ...
  });

ওয়েব মডুলার API

import { getFunctions, httpsCallable } from "firebase/functions";

const functions = getFunctions();
const addMessage = httpsCallable(functions, 'addMessage');
addMessage({ text: messageText })
  .then((result) => {
    // Read result of the Cloud Function.
    /** @type {any} */
    const data = result.data;
    const sanitizedMessage = data.text;
  })
  .catch((error) => {
    // Getting the Error details.
    const code = error.code;
    const message = error.message;
    const details = error.details;
    // ...
  });

Kotlin+KTX

addMessage(inputMessage)
    .addOnCompleteListener { task ->
        if (!task.isSuccessful) {
            val e = task.exception
            if (e is FirebaseFunctionsException) {
                val code = e.code
                val details = e.details
            }
        }
    }

Java

addMessage(inputMessage)
        .addOnCompleteListener(new OnCompleteListener<String>() {
            @Override
            public void onComplete(@NonNull Task<String> task) {
                if (!task.isSuccessful()) {
                    Exception e = task.getException();
                    if (e instanceof FirebaseFunctionsException) {
                        FirebaseFunctionsException ffe = (FirebaseFunctionsException) e;
                        FirebaseFunctionsException.Code code = ffe.getCode();
                        Object details = ffe.getDetails();
                    }
                }
            }
        });

Dart

try {
  final result =
      await FirebaseFunctions.instance.httpsCallable('addMessage').call();
} on FirebaseFunctionsException catch (error) {
  print(error.code);
  print(error.details);
  print(error.message);
}

সি++

void OnAddMessageCallback(
    const firebase::Future<firebase::functions::HttpsCallableResult>& future) {
  if (future.error() != firebase::functions::kErrorNone) {
    // Function error code, will be kErrorInternal if the failure was not
    // handled properly in the function call.
    auto code = static_cast<firebase::functions::Error>(future.error());

    // Display the error in the UI.
    DisplayError(code, future.error_message());
    return;
  }

  const firebase::functions::HttpsCallableResult* result = future.result();
  firebase::Variant data = result->data();
  // This will assert if the result returned from the function wasn't a string.
  std::string message = data.string_value();
  // Display the result in the UI.
  DisplayResult(message);
}

// ...

// ...
  auto future = AddMessage(message);
  future.OnCompletion(OnAddMessageCallback);
  // ...

ঐক্য

 addMessage(text).ContinueWith((task) => {
  if (task.IsFaulted) {
    foreach (var inner in task.Exception.InnerExceptions) {
      if (inner is FunctionsException) {
        var e = (FunctionsException) inner;
        // Function error code, will be INTERNAL if the failure
        // was not handled properly in the function call.
        var code = e.ErrorCode;
        var message = e.ErrorMessage;
      }
    }
  } else {
    string result = task.Result;
  }
});

আপনি আপনার অ্যাপ চালু করার আগে, শুধুমাত্র আপনার অ্যাপগুলি আপনার কলযোগ্য ফাংশন এন্ডপয়েন্টগুলি অ্যাক্সেস করতে পারে তা নিশ্চিত করতে সহায়তা করার জন্য আপনাকে অ্যাপ চেক সক্ষম করতে হবে।