কাস্টম ইভেন্ট ট্রিগার তৈরি করুন এবং পরিচালনা করুন

Cloud Functions (২য় জেনারেশন)-এর মাধ্যমে, আপনি কাস্টম ইভেন্টের প্রতিক্রিয়ায় ফাংশন ট্রিগার করতে পারেন। এই ইভেন্টগুলো বিশেষ বা অতিরিক্ত ইভেন্ট প্রোভাইডার দ্বারা সরবরাহ করা হয়, যা Cloud Functions জন্য Firebase এসডিকে দ্বারা নেটিভভাবে সমর্থিত ফায়ারবেস ইভেন্টগুলো থেকে ভিন্ন। কাস্টম ইভেন্ট ট্রিগারের মাধ্যমে, আপনার অ্যাপ Firebase Extensions দ্বারা সরবরাহ করা ইভেন্টগুলোতে সাড়া দিতে পারে, অথবা আপনি আপনার নিজস্ব কাস্টম ইভেন্ট প্রকাশ করে সেগুলোর প্রতিক্রিয়ায় ফাংশন ট্রিগার করতে পারেন।

সকল কাস্টম ইভেন্ট CloudEvents-এর JSON ইভেন্ট ফরম্যাট মেনে চলে এবং Eventarc এ প্রকাশিত হয়। Eventarc ব্যবহারের জন্য ফি প্রযোজ্য।

কাস্টম ইভেন্ট দিয়ে ফাংশন ট্রিগার করুন

এই মৌলিক প্রবাহটি বাস্তবায়নের মাধ্যমে আপনি কাস্টম ইভেন্ট প্রকাশ করতে (অথবা ফায়ারবেস এক্সটেনশন থেকে ইভেন্ট পেতে) এবং সেই ইভেন্টগুলির প্রতিক্রিয়ায় ফাংশন ট্রিগার করতে পারেন:

  1. কাঙ্ক্ষিত ইভেন্টগুলো একটি ইভেন্টআর্ক চ্যানেলে প্রকাশ করুন, অথবা আপনার ইনস্টল করা কোনো এক্সটেনশন দ্বারা প্রদত্ত উপলব্ধ ইভেন্টগুলো শনাক্ত করুন।
  2. আপনার ফাংশন কোডে একটি ইভেন্ট হ্যান্ডলার ব্যবহার করে ইভেন্টআর্ক চ্যানেলের ইভেন্টগুলোতে সাবস্ক্রাইব করুন।
  3. ফাংশনটির ভেতরে, CloudEvent অবজেক্টে ফেরত আসা পেলোডটি পার্স করুন এবং আপনার অ্যাপের প্রয়োজন অনুযায়ী কাস্টম লজিক প্রয়োগ করুন।

For example, a game app might want to send notifications to users as they enter or leave the leaderboard of top ten competitors. This app could publish leaderboard events to the default channel, and then handle the event in a function that sends targeted push notifications to users.

In another example, an extension designed to help apps process large images might emit an event on the completion of image resizing. Apps with this extension installed could handle the completion event by updating links in the app to point to resized versions of the image.

একটি চ্যানেলে একটি ইভেন্ট প্রকাশ করুন

Eventarc events are published into channels . Channels are a way to group related events and manage access permissions. When you install an extension or deploy a function that consumes custom events, Firebase automatically creates a default channel named firebase in the us-central1 region. The Firebase Admin SDK provides an eventarc subpackage for publishing to channels.

ডিফল্ট চ্যানেল ব্যবহার করে একটি বিশ্বস্ত সার্ভার (বা অন্য কোনো ফাংশন) থেকে কোনো ইভেন্ট প্রকাশ করতে:

import {getEventarc} from 'firebase-admin/eventarc';

getEventarc().channel().publish({
    type: 'achieved-leaderboard',
    subject: 'Welcome to the top 10',
    data: {
      message: 'You have achieved the nth position in our leaderboard!  To see . . .'
    }
});

In addition to automatically creating the default channel, Firebase sets the environment variable EVENTARC_CLOUD_EVENT_SOURCE , which specifies the source of the event. If you are publishing events outside of Cloud Functions for Firebase , you'll need to explicitly add the source field in your event payload.

কাস্টম ইভেন্টগুলি পরিচালনা করুন

You can handle all custom events, including extensions events, with the onCustomEventPublished or on_custom_event_published handlers. First, import this handler from the Eventarc SDK along with the Firebase Admin SDK :

নোড.জেএস

const {onCustomEventPublished} = require("firebase-functions/eventarc");
const logger = require("firebase-functions/logger");
const {initializeApp} = require("firebase-admin/app");
const {getFirestore} = require("firebase-admin/firestore");

পাইথন

from firebase_admin import firestore, initialize_app
from firebase_functions import eventarc_fn

আপনার ফাংশন কোডে, উদাহরণ ফাংশনে দেখানো অনুযায়ী ইভেন্টের নামটি পাস করুন:

নোড.জেএস

exports.onimageresized = onCustomEventPublished(
    "firebase.extensions.storage-resize-images.v1.complete",
    (event) => {
      logger.info("Received image resize completed event", event);
      // For example, write resized image details into Firestore.
      return getFirestore()
          .collection("images")
          .doc(event.subject.replace("/", "_")) // original file path
          .set(event.data); // resized images paths and sizes
    });

পাইথন

@eventarc_fn.on_custom_event_published(
    event_type="firebase.extensions.storage-resize-images.v1.complete"
)
def onimageresized(event: eventarc_fn.CloudEvent) -> None:
    print("Received image resize completed event: ", event.type)

    if not isinstance(event.subject, str):
        print("No 'subject' data.")
        return

    # For example, write resized image details into Firestore.
    firestore_client: google.cloud.firestore.Client = firestore.client()
    collection = firestore_client.collection("images")
    doc = collection.document(event.subject.replace("/", "_"))  # original file path
    doc.set(event.data)  # resized images paths and sizes

প্রতিটি নির্দিষ্ট এক্সটেনশনের জন্য, ইভেন্ট অবজেক্টে ফেরত আসা পেলোডটি এমন ডেটা সরবরাহ করে যা আপনি আপনার অ্যাপ্লিকেশন ফ্লো-এর জন্য কাস্টম লজিক সম্পাদন করতে ব্যবহার করতে পারেন। এই ক্ষেত্রে, ফাংশনটি Admin SDK ব্যবহার করে রিসাইজ করা ছবিটির মেটাডেটা Cloud Firestore একটি কালেকশনে কপি করে, ইভেন্ট দ্বারা প্রদত্ত subject থেকে ফাইলের নাম সংগ্রহ করে এবং ইভেন্ট থেকে প্রাপ্ত data থেকে মেটাডেটা সংরক্ষণ করে।

নন-ডিফল্ট চ্যানেলগুলিতে ইভেন্ট প্রকাশ ও পরিচালনা করুন

Custom channels can be useful for cases where you have special permission needs or other requirements, and don't want the same level of visibility and access for all events. You can create your own channels using the Google Cloud console . Publishing and subscribing for events must be done on the same channel.

In cases where a custom event is published on a non-default channel, you'll need to specify the channel in your function code. For example, if you want to handle events that are published in a non-default channel for the us-west1 location, you need to specify the channel as shown:

নোড.জেএস

import { onCustomEventPublished } from "firebase-functions/v2/eventarc";

export const func = onCustomEventPublished(
    {
      eventType: "firebase.extensions.storage-resize-images.v1.complete",
      channel: "locations/us-west1/channels/firebase",
      region: "us-west1",
    },
    (event) => { ... });

পাইথন

@eventarc_fn.on_custom_event_published(
    event_type="firebase.extensions.storage-resize-images.v1.complete",
    channel="locations/us-west1/channels/firebase",
    region="us-west1",
)
def onimageresizedwest(event: eventarc_fn.CloudEvent) -> None:
    print("Received image resize completed event: ", event.type)
    # ...