आपके ऐप्लिकेशन में कॉल करने की सुविधा (पहली जनरेशन)

Cloud Functions for Firebase क्लाइंट SDK टूल की मदद से, सीधे Firebase ऐप्लिकेशन से फ़ंक्शन कॉल किए जा सकते हैं. अपने ऐप्लिकेशन से इस तरह से फ़ंक्शन कॉल करने के लिए, Cloud Functions में एचटीटीपी कॉलेबल फ़ंक्शन लिखें और उसे डिप्लॉय करें. इसके बाद, अपने ऐप्लिकेशन से फ़ंक्शन को कॉल करने के लिए क्लाइंट लॉजिक जोड़ें.

यह ध्यान रखना ज़रूरी है कि एचटीटीपी कॉल किए जा सकने वाले फ़ंक्शन, एचटीटीपी फ़ंक्शन से मिलते-जुलते होते हैं, लेकिन उनके जैसे नहीं होते. एचटीटीपी कॉलेबल फ़ंक्शन का इस्तेमाल करने के लिए, आपको अपने प्लैटफ़ॉर्म के लिए क्लाइंट एसडीके का इस्तेमाल करना होगा. साथ ही, बैकएंड एपीआई का इस्तेमाल करना होगा या प्रोटोकॉल को लागू करना होगा. कॉल किए जा सकने वाले फ़ंक्शन, एचटीटीपी फ़ंक्शन से इन मामलों में अलग होते हैं:

  • कॉल किए जा सकने वाले फ़ंक्शन के साथ, Firebase Authentication टोकन, FCM टोकन, और App Check टोकन, उपलब्ध होने पर अनुरोधों में अपने-आप शामिल हो जाते हैं.
  • ट्रिगर, अनुरोध के मुख्य हिस्से को अपने-आप डीसीरियलाइज़ करता है और पुष्टि करने वाले टोकन की पुष्टि करता है.

Cloud Functions की दूसरी जनरेशन और उसके बाद के वर्शन के लिए Firebase SDK टूल, Firebase क्लाइंट SDK टूल के इन कम से कम वर्शन के साथ काम करता है, ताकि एचटीटीपीएस कॉलेबल फ़ंक्शन काम कर सकें:

  • Apple प्लैटफ़ॉर्म के लिए Firebase SDK टूल का वर्शन 13.0.0
  • Android 23.0.0 के लिए Firebase SDK
  • Firebase Modular Web SDK v. 9.7.0

अगर आपको किसी ऐसे प्लैटफ़ॉर्म पर बनाए गए ऐप्लिकेशन में मिलती-जुलती सुविधा जोड़नी है जो काम नहीं करता, तो https.onCall के लिए प्रोटोकॉल स्पेसिफ़िकेशन देखें. इस गाइड में, Apple प्लैटफ़ॉर्म, Android, वेब, C++, और Unity के लिए, एचटीटीपी कॉल किए जा सकने वाले फ़ंक्शन को लिखने, डिप्लॉय करने, और कॉल करने के तरीके के बारे में बताया गया है.

कॉल किए जा सकने वाले फ़ंक्शन को लिखना और डिप्लॉय करना

एचटीटीपीएस कॉलेबल फ़ंक्शन बनाने के लिए, 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) => {
    // ...
  });
  

कॉल किए जा सकने वाले ऐसे फ़ंक्शन के लिए जो Realtime Database में टेक्स्ट मैसेज सेव करता है, उदाहरण के लिए, data में मैसेज का टेक्स्ट शामिल हो सकता है. वहीं, context पैरामीटर, उपयोगकर्ता की पुष्टि करने की जानकारी दिखाते हैं:

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

कॉल किए जा सकने वाले फ़ंक्शन की जगह और कॉल करने वाले क्लाइंट की जगह के बीच की दूरी की वजह से, नेटवर्क में इंतज़ार का समय बढ़ सकता है. परफ़ॉर्मेंस को ऑप्टिमाइज़ करने के लिए, जहां लागू हो वहां फ़ंक्शन की जगह की जानकारी दें. साथ ही, पक्का करें कि कॉल किए जा सकने वाले फ़ंक्शन की जगह की जानकारी, क्लाइंट साइड पर एसडीके को शुरू करते समय सेट की गई जगह की जानकारी से मेल खाती हो.

इसके अलावा, आपके पास App Check अटेस्टेशन अटैच करने का विकल्प भी होता है. इससे आपके बैकएंड संसाधनों को बिलिंग की धोखाधड़ी या फ़िशिंग जैसे गलत इस्तेमाल से बचाने में मदद मिलती है. Cloud Functions के लिए App Check को लागू करने की सुविधा चालू करें देखें.

नतीजा वापस भेजना

क्लाइंट को डेटा वापस भेजने के लिए, ऐसा डेटा दिखाएं जिसे JSON कोड में बदला जा सके. उदाहरण के लिए, जोड़ने की कार्रवाई का नतीजा दिखाने के लिए:

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

एसिंक्रोनस ऑपरेशन के बाद डेटा वापस पाने के लिए, प्रॉमिस का इस्तेमाल करें. प्रॉमिस से मिला डेटा, क्लाइंट को वापस भेज दिया जाता है. उदाहरण के लिए, ऐसा टेक्स्ट दिखाया जा सकता है जिसे कॉल किए जा सकने वाले फ़ंक्शन ने Realtime Database में लिखा है:

// Saving the new message to the Realtime Database.
const sanitizedMessage = sanitizer.sanitizeText(text); // Sanitize message.

return getDatabase().ref("/messages").push({
  text: sanitizedMessage,
  author: {uid, name, picture, email},
}).then(() => {
  logger.info("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 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 (!request.auth) {
  // Throwing an HttpsError so that the client gets the error details.
  throw new HttpsError("failed-precondition", "The function must be " +
          "called while authenticated.");
}

कॉल किए जा सकने वाले फ़ंक्शन को डिप्लॉय करना

index.js में कॉल किए जा सकने वाले फ़ंक्शन को सेव करने के बाद, firebase deploy को चलाने पर, यह अन्य सभी फ़ंक्शन के साथ डिप्लॉय हो जाता है. सिर्फ़ कॉल किए जा सकने वाले फ़ंक्शन को डिप्लॉय करने के लिए, --only आर्ग्युमेंट का इस्तेमाल करें. इससे आंशिक डिप्लॉयमेंट किया जा सकता है:

firebase deploy --only functions:addMessage

अगर फ़ंक्शन डिप्लॉय करते समय आपको अनुमति से जुड़ी गड़बड़ियां मिलती हैं, तो पक्का करें कि डिप्लॉयमेंट कमांड चलाने वाले उपयोगकर्ता को सही आईएएम भूमिकाएं असाइन की गई हों.

क्लाइंट डेवलपमेंट एनवायरमेंट सेट अप करना

पक्का करें कि आपने सभी ज़रूरी शर्तें पूरी की हों. इसके बाद, अपने ऐप्लिकेशन में ज़रूरी डिपेंडेंसी और क्लाइंट लाइब्रेरी जोड़ें.

iOS+

अपने Apple ऐप्लिकेशन में Firebase जोड़ने के लिए, निर्देशों का पालन करें.

Firebase डिपेंडेंसी इंस्टॉल और मैनेज करने के लिए, Swift Package Manager का इस्तेमाल करें.

  1. Xcode में, अपना ऐप्लिकेशन प्रोजेक्ट खोलें. इसके बाद, File > Add Packages पर जाएं.
  2. प्रॉम्प्ट मिलने पर, Firebase Apple प्लैटफ़ॉर्म SDK टूल की रिपॉज़िटरी जोड़ें:
  3.   https://github.com/firebase/firebase-ios-sdk.git
  4. Cloud Functions लाइब्रेरी चुनें.
  5. अपने टारगेट की बिल्ड सेटिंग के अन्य लिंकर फ़्लैग सेक्शन में, -ObjC फ़्लैग जोड़ें.
  6. इसके बाद, Xcode बैकग्राउंड में आपकी डिपेंडेंसी से जुड़ी समस्या को हल करना और उन्हें डाउनलोड करना शुरू कर देगा.

Web

  1. अपने वेब ऐप्लिकेशन में Firebase जोड़ने के लिए, निर्देशों का पालन करें. पक्का करें कि आपने अपने टर्मिनल से यह कमांड चलाया हो:
    npm install firebase@13.0.0 --save
  2. Firebase कोर और Cloud Functions, दोनों को मैन्युअल तरीके से शामिल करना ज़रूरी है:

     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);

Web

  1. अपने वेब ऐप्लिकेशन में Firebase जोड़ने के लिए, निर्देशों का पालन करें.
  2. अपने ऐप्लिकेशन में Firebase कोर और Cloud Functions क्लाइंट लाइब्रेरी जोड़ें:
    <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>

Cloud Functions SDK टूल, npm पैकेज के तौर पर भी उपलब्ध है.

  1. अपने टर्मिनल से यह कमांड चलाएं:
    npm install firebase@8.10.1 --save
  2. Firebase कोर और Cloud Functions, दोनों को मैन्युअल तरीके से शामिल करना ज़रूरी है:
    const firebase = require("firebase");
    // Required for side-effects
    require("firebase/functions");

Kotlin

  1. अपने Android ऐप्लिकेशन में Firebase जोड़ने के लिए, निर्देशों का पालन करें.

  2. मॉड्यूल (ऐप्लिकेशन-लेवल) की Gradle फ़ाइल (आम तौर पर <project>/<app-module>/build.gradle.kts या <project>/<app-module>/build.gradle) में, Android के लिए Cloud Functions लाइब्रेरी की डिपेंडेंसी जोड़ें. हमारा सुझाव है कि लाइब्रेरी के वर्शन को कंट्रोल करने के लिए, Firebase Android BoM का इस्तेमाल करें.

    dependencies {
        // Import the BoM for the Firebase platform
        implementation(platform("com.google.firebase:firebase-bom:35.0.2"))
    
        // 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 लाइब्रेरी के हर वर्शन को उसकी डिपेंडेंसी लाइन में बताना होगा.

    ध्यान दें कि अगर आपके ऐप्लिकेशन में एक से ज़्यादा 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:23.0.0")
    }

Java

  1. अपने Android ऐप्लिकेशन में Firebase जोड़ने के लिए, निर्देशों का पालन करें.

  2. मॉड्यूल (ऐप्लिकेशन-लेवल) की Gradle फ़ाइल (आम तौर पर <project>/<app-module>/build.gradle.kts या <project>/<app-module>/build.gradle) में, Android के लिए Cloud Functions लाइब्रेरी की डिपेंडेंसी जोड़ें. हमारा सुझाव है कि लाइब्रेरी के वर्शन को कंट्रोल करने के लिए, Firebase Android BoM का इस्तेमाल करें.

    dependencies {
        // Import the BoM for the Firebase platform
        implementation(platform("com.google.firebase:firebase-bom:35.0.2"))
    
        // 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 लाइब्रेरी के हर वर्शन को उसकी डिपेंडेंसी लाइन में बताना होगा.

    ध्यान दें कि अगर आपके ऐप्लिकेशन में एक से ज़्यादा 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:23.0.0")
    }

Dart

  1. अपने Flutter ऐप्लिकेशन में Firebase जोड़ने के लिए, निर्देशों का पालन करें.

  2. प्लगिन इंस्टॉल करने के लिए, अपने Flutter प्रोजेक्ट के रूट से यह कमांड चलाएं:

    flutter pub add cloud_functions
    
  3. इसके बाद, अपने Flutter ऐप्लिकेशन को फिर से बनाएं:

    flutter run
    
  4. इंस्टॉल होने के बाद, cloud_functions प्लग इन को अपने Dart कोड में इंपोर्ट करके ऐक्सेस किया जा सकता है:

    import 'package:cloud_functions/cloud_functions.dart';
    

C++

Android के साथ C++ के लिए:

  1. अपने C++ प्रोजेक्ट में Firebase जोड़ने के लिए, दिए गए निर्देशों का पालन करें.
  2. firebase_functions लाइब्रेरी को अपनी CMakeLists.txt फ़ाइल में जोड़ें.

Apple प्लैटफ़ॉर्म के साथ C++ के लिए:

  1. अपने C++ प्रोजेक्ट में Firebase जोड़ने के लिए, दिए गए निर्देशों का पालन करें.
  2. Cloud Functions पॉड को अपने Podfile में जोड़ें:
    pod 'Firebase/Functions'
  3. फ़ाइल सेव करें. इसके बाद, यह कमांड चलाएं:
    pod install
  4. अपने Xcode प्रोजेक्ट में, Firebase C++ SDK टूल से Firebase कोर और Cloud Functions फ़्रेमवर्क जोड़ें.
    • firebase.framework
    • firebase_functions.framework

Unity

  1. अपने Unity प्रोजेक्ट में Firebase जोड़ने के लिए, दिए गए निर्देशों का पालन करें.
  2. अपने Unity प्रोजेक्ट में, Firebase Unity SDK से FirebaseFunctions.unitypackage जोड़ें.

क्लाइंट एसडीके को शुरू करना

Cloud Functions का कोई इंस्टेंस शुरू करें:

Swift

lazy var functions = Functions.functions()

Objective-C

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

Web

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();

Web

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

Kotlin

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

Java

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

Dart

final functions = FirebaseFunctions.instance;

C++

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

Unity

functions = Firebase.Functions.DefaultInstance;

फ़ंक्शन को कॉल करना

Swift

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
  }
}

Objective-C

[[_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"];
}];

Web

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

Web

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

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;

C++

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);
}

Unity

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 शामिल होता है. कॉल किए जा सकने वाले फ़ंक्शन में गड़बड़ियों को ठीक करने का तरीका जानने के लिए, दिशा-निर्देश देखें.

Swift

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]
  }
  // ...
}

Objective-C

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

Web

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;
    // ...
  });

Web

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

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);
}

C++

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);
  // ...

Unity

 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;
  }
});

क्लाइंट टाइम आउट सेट करना

डिफ़ॉल्ट रूप से, Cloud Functions क्लाइंट SDK टूल, कॉल किए जा सकने वाले अनुरोधों के लिए 70 सेकंड का टाइम आउट इस्तेमाल करते हैं. अगर कॉल किए जा सकने वाले फ़ंक्शन को पूरा होने में 70 सेकंड से ज़्यादा समय लगता है, तो क्लाइंट एसडीके, अनुरोध को DEADLINE_EXCEEDED (deadline-exceeded) गड़बड़ी के साथ खत्म कर देता है. भले ही, आपने सर्वर साइड पर टाइम आउट को ज़्यादा समय के लिए कॉन्फ़िगर किया हो.

लंबे समय तक चलने वाले कॉलेबल फ़ंक्शन के लिए, आपको सर्वर और क्लाइंट, दोनों पर टाइम आउट की अवधि बढ़ानी होगी:

  1. सर्वर-साइड फ़ंक्शन के टाइम आउट को बढ़ाएं (टाइम आउट और मेमोरी का बंटवारा सेट करना देखें).
  2. अपने ऐप्लिकेशन में, कॉल किए जा सकने वाले रेफ़रंस को कॉन्फ़िगर करते समय, क्लाइंट-साइड टाइम आउट बढ़ाएं:

Swift

let callable = functions.httpsCallable("addMessage")
callable.timeoutInterval = 300  // 300 seconds (default is 70)
let result = try await callable.call(["text": text, "push": true])

Objective-C

FIRHTTPSCallable *callable = [self.functions HTTPSCallableWithName:@"addMessage"];
callable.timeoutInterval = 300;  // 300 seconds (default is 70)
[callable callWithObject:@{@"text": text, @"push": @YES}
              completion:^(FIRHTTPSCallableResult * _Nullable result, NSError * _Nullable error) {
  // ...
}];

Web

var addMessage = firebase.functions().httpsCallable('addMessage', {
  timeout: 300000,  // 300 seconds in milliseconds (default is 70000)
});
addMessage({ text: messageText })
  .then((result) => {
    // ...
  });

Web

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

const functions = getFunctions();
const addMessage = httpsCallable(functions, 'addMessage', {
  timeout: 300000,  // 300 seconds in milliseconds (default is 70000)
});
const result = await addMessage({ text: messageText });

Kotlin

val callable = functions.getHttpsCallable("addMessage")
callable.setTimeout(300L, TimeUnit.SECONDS)
val result = callable.call(data).await()

Java

HttpsCallableReference callable = mFunctions.getHttpsCallable("addMessage");
callable.setTimeout(300L, TimeUnit.SECONDS);
Task<HttpsCallableResult> result = callable.call(data);

Dart

final callable = FirebaseFunctions.instance.httpsCallable(
  'addMessage',
  options: HttpsCallableOptions(
    timeout: const Duration(seconds: 300),
  ),
);
final result = await callable.call({
  'text': text,
  'push': true,
});

सुझाव: App Check की मदद से गलत इस्तेमाल को रोकना

ऐप्लिकेशन लॉन्च करने से पहले, App Check को चालू करें. इससे यह पक्का करने में मदद मिलती है कि सिर्फ़ आपके ऐप्लिकेशन, कॉल किए जा सकने वाले फ़ंक्शन एंडपॉइंट को ऐक्सेस कर पाएं.