Cloud Functions for Firebase ক্লায়েন্ট SDK আপনাকে সরাসরি Firebase অ্যাপ থেকে ফাংশন কল করতে দেয়। এইভাবে আপনার অ্যাপ থেকে কোনও ফাংশন কল করতে, Cloud Functions-এ HTTP কল করা যায় এমন ফাংশন লিখুন ও প্রয়োগ করুন এবং তারপরে আপনার অ্যাপ থেকে ফাংশন কল করার জন্য ক্লায়েন্ট লজিক যোগ করুন।
মনে রাখা গুরুত্বপূর্ণ যে HTTP কল করা যায় এমন ফাংশনগুলি একই রকম হলেও HTTP ফাংশনের সাথে একই নয়। HTTP কল করা যায় এমন ফাংশন ব্যবহার করতে হলে আপনাকে অবশ্যই ব্যাকএন্ড API (বা প্রোটোকল প্রয়োগ করুন)-এর সাথে আপনার প্ল্যাটফর্মের জন্য ক্লায়েন্ট SDK ব্যবহার করতে হবে। কল করার উপযুক্ত ফাংশন ও HTTP ফাংশনের মধ্যে এইসব মূল পার্থক্য রয়েছে:
- কলযোগ্য, Firebase Authentication টোকেন, FCM টোকেন ও App Check টোকেন উপলভ্য থাকলে, সেগুলি অটোমেটিক অনুরোধে অন্তর্ভুক্ত করা হয়।
- ট্রিগারটি অটোমেটিক অনুরোধের বডিকে ডিসিরিয়ালাইজ করে এবং যাচাইকরণ টোকেনকে যাচাই করে।
HTTPS কল করার উপযুক্ত ফাংশন কাজ করানোর জন্য Firebase 2nd gen ও তার পরের ভার্সনের SDK Cloud Functions-এর সাথে এইসব Firebase ক্লায়েন্ট SDK-এর ন্যূনতম ভার্সন ইন্টারঅপারেট করে:
- Firebase Apple প্ল্যাটফর্মের জন্য SDK 13.0.0
- Firebase Android 22.1.1-এর জন্য SDK
- Firebase Modular Web SDK v. 9.7.0
আপনি যদি কোনও কাজ করে না এমন
প্ল্যাটফর্মে তৈরি অ্যাপে একই ধরনের ফাংশনালিটি যোগ করতে চান, তাহলে https.onCall-এর জন্য প্রোটোকল স্পেসিফিকেশন দেখুন। এই গাইডের বাকি অংশে Apple প্ল্যাটফর্ম, Android, ওয়েব, C++ ও Unity-র জন্য
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) => { // ... });
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;
কল করা যায় এমন ফাংশনের লোকেশন এবং কলিং ক্লায়েন্টের লোকেশনের মধ্যবর্তী দূরত্ব নেটওয়ার্ক লেটেন্সি তৈরি করতে পারে। পারফর্ম্যান্স অপ্টিমাইজ করতে, যেখানে প্রযোজ্য সেখানে ফাংশন লোকেশন নির্দিষ্ট করার কথা বিবেচনা করুন এবং ক্লায়েন্ট-সাইডে SDK ইনিশিয়ালাইজ করার সময় সেট করা লোকেশনের সাথে কল করার উপযুক্ত লোকেশন অ্যালাইন করতে ভুলবেন না।
বিকল্প হিসেবে, আপনি 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
ফাংশন ডেপ্লয় করার সময় অনুমতির ব্যাপারে কোনও সমস্যা হলে, নিশ্চিত করুন যে ডেপ্লয়মেন্ট কমান্ড রান করা ব্যবহারকারীকে উপযুক্ত IAM ভূমিকা অ্যাসাইন করা হয়েছে।
আপনার ক্লায়েন্ট ডেভেলপমেন্ট এনভায়রনমেন্ট সেট-আপ করা
আপনি পূর্বশর্ত পূরণ করেছেন কিনা তা নিশ্চিত করুন, তারপর আপনার অ্যাপে প্রয়োজনীয় ডিপেন্ডেন্সি এবং ক্লায়েন্ট লাইব্রেরি যোগ করুন।
iOS+
আপনার Apple অ্যাপে Firebase যোগ করতে নির্দেশাবলী অনুসরণ করুন।
Firebase ডিপেন্ডেন্সি ইনস্টল ও ম্যানেজ করতে Swift Package Manager ব্যবহার করুন।
- Xcode-এ, আপনার অ্যাপ প্রোজেক্ট খোলা থাকা অবস্থায়, ফাইল > প্যাকেজ যোগ করুন বিকল্পে নেভিগেট করুন।
- প্রম্পট করা হলে, Firebase Apple প্ল্যাটফর্ম SDK রিপোজিটরি যোগ করুন:
- Cloud Functions লাইব্রেরি বেছে নিন।
- আপনার টার্গেটের বিল্ড সেটিংসের অন্যান্য লিঙ্কার ফ্ল্যাগ বিভাগে
-ObjCফ্ল্যাগ যোগ করুন। - এটি সম্পূর্ণ হয়ে গেলে, Xcode অটোমেটিক আপনার ডিপেন্ডেন্সি ব্যাকগ্রাউন্ডে সমাধান ও ডাউনলোড করা শুরু করবে।
https://github.com/firebase/firebase-ios-sdk.git
Web
-
আপনার ওয়েব অ্যাপে Firebase যোগ করতে নির্দেশাবলী অনুসরণ করুন। আপনার টার্মিনাল থেকে
নিম্নলিখিত কমান্ড রান করতে ভুলবেন না:
npm install firebase@12.19.0 --save
ম্যানুয়ালি Firebase core ও 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
- আপনার ওয়েব অ্যাপে Firebase যোগ করতে নির্দেশাবলী অনুসরণ করুন।
- আপনার অ্যাপে Firebase core ও 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 প্যাকেজ হিসেবেও উপলভ্য।
- আপনার টার্মিনাল থেকে নিম্নলিখিত কমান্ড রান করুন:
npm install firebase@8.10.1 --save
- ম্যানুয়ালি Firebase core ও Cloud Functions দুটিই প্রয়োজন:
const firebase = require("firebase"); // Required for side-effects require("firebase/functions");
Kotlin
আপনার Android অ্যাপে Firebase যোগ করতে নির্দেশাবলী অনুসরণ করুন।
আপনার মডিউল (অ্যাপ-লেভেল) 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:34.19.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 লাইব্রেরি ভার্সন উল্লেখ করতে হবে।
মনে রাখবেন, আপনার অ্যাপে একাধিক 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:22.1.1") }
Java
আপনার Android অ্যাপে Firebase যোগ করতে নির্দেশাবলী অনুসরণ করুন।
আপনার মডিউল (অ্যাপ-লেভেল) 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:34.19.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 লাইব্রেরি ভার্সন উল্লেখ করতে হবে।
মনে রাখবেন, আপনার অ্যাপে একাধিক 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:22.1.1") }
Dart
আপনার Flutter অ্যাপে Firebase যোগ করতে নির্দেশাবলী অনুসরণ করুন।
আপনার Flutter প্রোজেক্টের রুট থেকে, প্লাগ-ইন ইনস্টল করতে নিচের কমান্ডটি রান করুন:
flutter pub add cloud_functionsসম্পূর্ণ হয়ে গেলে, আপনার Flutter অ্যাপ্লিকেশন আবার তৈরি করুন:
flutter runইনস্টল করা হয়ে গেলে, আপনার Dart কোডে ইমপোর্ট করে
cloud_functionsপ্লাগ-ইন অ্যাক্সেস করতে পারবেন:import 'package:cloud_functions/cloud_functions.dart';
C++
Android-এর সাথে C++-এর জন্য:
- আপনার C++ প্রোজেক্টে Firebase যোগ করতে নির্দেশাবলী অনুসরণ করুন।
- আপনার
CMakeLists.txtফাইলেfirebase_functionsলাইব্রেরি যোগ করুন।
Apple প্ল্যাটফর্মের সাথে C++-এর জন্য:
- আপনার C++ প্রোজেক্টে Firebase যোগ করতে নির্দেশাবলী অনুসরণ করুন।
- আপনার
Podfile-এ Cloud Functions পড যোগ করুন:pod 'Firebase/Functions'
- ফাইলটি সেভ করে, তারপর এটি রান করান:
pod install
- আপনার Xcode প্রোজেক্টে Firebase কোর ও Cloud Functions ফ্রেমওয়ার্ক যোগ করুন।
Firebase C++ SDK
firebase.frameworkfirebase_functions.framework
Unity
- নির্দেশাবলী অনুসরণ করে আপনার Unity প্রোজেক্টে Firebase যোগ করুন।
- আপনার Unity প্রোজেক্টে Firebase Unity SDK থেকে
FirebaseFunctions.unitypackageযোগ করুন।
ক্লায়েন্ট SDK শুরু করা
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 কল করা যায় এমন অনুরোধের জন্য
৭০ সেকেন্ডের টাইম-আউট ব্যবহার করে। কল করা যায় এমন কোনও ফাংশন সম্পূর্ণ হতে ৭০ সেকেন্ডের বেশি সময়
লাগলে, ক্লায়েন্ট SDK DEADLINE_EXCEEDED
(deadline-exceeded) সমস্যা সহ অনুরোধটি বাতিল করে দেয়—এমনকি আপনি যদি আরও বেশি সময় ধরে সার্ভার-সাইড টাইম-আউট কনফিগার করে থাকেন।
দীর্ঘ সময় ধরে কল করা যায় এমন ফাংশন যাতে কাজ করে, তার জন্য আপনাকে সার্ভার ও ক্লায়েন্ট দুটি জায়গাতেই টাইম-আউট বাড়াতে হবে:
- সার্ভার-সাইড ফাংশন টাইমআউট বাড়ান (দেখুন টাইমআউট ও মেমরি অ্যালোকেশন সেট করুন)।
- আপনার অ্যাপে কলযোগ্য রেফারেন্স কনফিগার করার সময় ক্লায়েন্ট-সাইড টাইম-আউট বাড়ান:
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 চালু করুন যাতে শুধুমাত্র আপনার অ্যাপই কল করা যায় এমন ফাংশন এন্ডপয়েন্ট অ্যাক্সেস করতে পারে।