किसी एक्सटेंशन के लिए Cloud Functions लिखना

एक्सटेंशन बनाते समय, Cloud Functions का इस्तेमाल करके उसका लॉजिक लिखा जाता है. यह ठीक उसी तरह से किया जाता है जिस तरह से कोई ऐसा फ़ंक्शन लिखा जाता है जिसका इस्तेमाल सिर्फ़ आपके प्रोजेक्ट में किया जाएगा. अपने फ़ंक्शन, extension.yaml फ़ाइल में तय किए जाते हैं. जब उपयोगकर्ता आपका एक्सटेंशन इंस्टॉल करते हैं, तब ये फ़ंक्शन उनके प्रोजेक्ट में डिप्लॉय हो जाते हैं.

Cloud Functions को इस्तेमाल करने के बारे में सामान्य जानकारी के लिए, Cloud Functions दस्तावेज़ देखें.

पहली और दूसरी जनरेशन Cloud Functions

Firebase, पहली और दूसरी जनरेशन, Cloud Functionsदोनों के साथ काम करता है. हालांकि, फ़िलहाल Firebase एक्सटेंशन पर कुछ पाबंदियां हैं. इनके तहत, यह तय किया जाता है कि कुछ ट्रिगर टाइप के साथ, क्लाउड फ़ंक्शन के किस जनरेशन का इस्तेमाल किया जा सकता है. इस वजह से, कई एक्सटेंशन में पहली और दूसरी जनरेशन के फ़ंक्शन शामिल होते हैं.

नीचे, हर ट्रिगर टाइप के लिए फ़ंक्शन जनरेशन की सुविधा के बारे में बताया गया है.

ध्यान देने वाली खास बातें

  • कुछ फ़ंक्शन की परिभाषाओं के लिए, आपको ऐसी जानकारी देनी होगी जो extension.yaml फ़ाइल में भी दी गई है. उदाहरण के लिए, Cloud Firestore में एक document() तरीका है, जो निगरानी के लिए दस्तावेज़ का पैटर्न तय करता है. साथ ही, extension.yaml में इसके एलान में एक resource फ़ील्ड होता है, जो इसी पैटर्न को तय करता है.

    इन स्थितियों में, extension.yaml फ़ाइल में दिए गए कॉन्फ़िगरेशन का इस्तेमाल किया जाता है. साथ ही, फ़ंक्शन की परिभाषा में दिए गए कॉन्फ़िगरेशन को अनदेखा कर दिया जाता है.

    दस्तावेज़ के लिए, फ़ंक्शन की परिभाषा में कॉन्फ़िगर की गई वैल्यू को शामिल करना एक सामान्य तरीका है. इस पेज पर दिए गए उदाहरण, इस पैटर्न को फ़ॉलो करते हैं.

  • Cloud Functions पहली जनरेशन के SDK में functions.config() तरीका और functions:config:set सीएलआई कमांड होती है. इनका इस्तेमाल, पहली जनरेशन के फ़ंक्शन में पैरामीटर वाली वैल्यू के साथ काम करने के लिए किया जा सकता है. इस तकनीक के इस्तेमाल पर Cloud Functions में रोक लगा दी गई है. साथ ही, यह एक्सटेंशन में काम नहीं करेगी. इसके बजाय, functions.params मॉड्यूल (सुझाया गया) या process.env का इस्तेमाल करें.

TypeScript का इस्तेमाल करना

अपना एक्सटेंशन डेवलप करने से जुड़े ज़्यादातर दस्तावेज़ों में, Cloud Functions for Firebase के लिए JavaScript का इस्तेमाल करके वर्कफ़्लो के बारे में बताया गया है. हालांकि, TypeScript का इस्तेमाल करके अपने फ़ंक्शन लिखे जा सकते हैं.

दरअसल, सभी आधिकारिक Firebase एक्सटेंशन TypeScript में लिखे गए हैं. अपने एक्सटेंशन के लिए TypeScript का इस्तेमाल करने के सबसे सही तरीकों के बारे में जानने के लिए, उन एक्सटेंशन की समीक्षा की जा सकती है.

अगर आपको अपने एक्सटेंशन के फ़ंक्शन TypeScript में लिखने हैं, तो एक्सटेंशन इंस्टॉल करने से पहले आपको यह काम करना होगा:

  1. अपने एक्सटेंशन के फ़ंक्शन के सोर्स कोड को JavaScript में कंपाइल करें.

    firebase ext:dev:init command की मदद से, फ़ंक्शन लिखने के लिए TypeScript को चुना जा सकता है. इस कमांड से, आपको इंस्टॉल किया जा सकने वाला पूरा एक्सटेंशन मिलता है. साथ ही, एक बिल्ड स्क्रिप्ट भी मिलती है, जिसे npm run build की मदद से चलाया जा सकता है.

  2. अपनी package.json फ़ाइल में, पक्का करें कि main फ़ील्ड, जनरेट किए गए JavaScript पर पॉइंट कर रहा हो.

  3. अगर आपको लोकल सोर्स से एक्सटेंशन इंस्टॉल या अपलोड करना है, तो पहले अपनी TypeScript फ़ाइलों को कंपाइल करें.

फ़ंक्शन ट्रिगर करने की सुविधा

एचटीटीपी ट्रिगर

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

एचटीटीपी ट्रिगर किए गए फ़ंक्शन लिखने के बारे में जानकारी के लिए, Cloud Functions दस्तावेज़ में एचटीटीपी अनुरोधों के ज़रिए फ़ंक्शन कॉल करना लेख पढ़ें.

फ़ंक्शन की परिभाषा (सिर्फ़ पहली जनरेशन के लिए)

import { https } from "firebase-functions/v1";

export const yourFunctionName = https.onRequest(async (req, resp) => {
  // ...
});

संसाधन का एलान (extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      runtime: nodejs16
      httpsTrigger: {}
  - name: anotherFunction
    type: ...

कॉल किए जा सकने वाले फ़ंक्शन

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

कॉल किए जा सकने वाले फ़ंक्शन इस्तेमाल करने के बारे में जानकारी पाने के लिए, Cloud Functions के दस्तावेज़ में आपके ऐप्लिकेशन से कॉल किए जा सकने वाले फ़ंक्शन देखें.

फ़ंक्शन की परिभाषा (सिर्फ़ पहली जनरेशन के लिए)

import { https } from "firebase-functions/v1";

export const yourFunctionName = https.onCall(async (data, context) => {
  // ...
});

संसाधन का एलान (extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      runtime: nodejs16
      httpsTrigger: {}
  - name: anotherFunction
    type: ...

शेड्यूल किए गए फ़ंक्शन ट्रिगर

शेड्यूल किया गया फ़ंक्शन, पसंद के मुताबिक बनाए गए शेड्यूल के आधार पर बार-बार चलता है.

शेड्यूल किए गए फ़ंक्शन लिखने के बारे में जानकारी पाने के लिए, Cloud Functions के दस्तावेज़ में शेड्यूल किए गए फ़ंक्शन देखें.

फ़ंक्शन की परिभाषा (सिर्फ़ पहली जनरेशन के लिए)

import { pubsub } from "firebase-functions/v1";

export const yourFunctionName = pubsub.schedule("every 6 hours").onRun((context) => {
  // ...
});

संसाधन का एलान (extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      scheduleTrigger:
        schedule: 'every 5 minutes'
  - name: anotherFunction
    type: ...

scheduleTrigger के लिए उपलब्ध सब-फ़ील्ड यहां दिए गए हैं:

फ़ील्ड ब्यौरा
schedule
(ज़रूरी है)

वह फ़्रीक्वेंसी जिस पर आपको फ़ंक्शन चलाना है.

इस फ़ील्ड में ऐसी स्ट्रिंग स्वीकार की जा सकती हैं जिनमें इनमें से किसी भी सिंटैक्स का इस्तेमाल किया गया हो (सिंगल-कोट में रैप करना ज़रूरी है):

timeZone
(ज़रूरी नहीं)

वह टाइम ज़ोन जिसके हिसाब से शेड्यूल किया गया है.

अगर आपको उपयोगकर्ताओं को एक्सटेंशन इंस्टॉल करते समय शेड्यूल कॉन्फ़िगर करने की सुविधा देनी है, तो अपनी extension.yaml फ़ाइल में एक नया पैरामीटर जोड़ें. साथ ही, अपने फ़ंक्शन के extension.yaml एलान में पैरामीटर का रेफ़रंस दें:resource

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      scheduleTrigger:
        schedule: ${SCHEDULE_FREQUENCY}
  - name: anotherFunction
    type: ...

params:
  - param: SCHEDULE_FREQUENCY
    label: Schedule
    description: How often do you want to run yourFunctionName()?
    type: string
    default: 'every 5 minutes'  # Specifying a default is optional.
    required: true

टास्क क्यू ट्रिगर

टास्क क्यू फ़ंक्शन, आपके एक्सटेंशन के लाइफ़साइकल इवेंट पर ट्रिगर होता है. इसके अलावा, इसे Admin SDK के TaskQueue.enqueue() तरीके का इस्तेमाल करके, एक्सटेंशन की टास्क क्यू में मैन्युअल तरीके से भी जोड़ा जा सकता है.

लाइफ़साइकल इवेंट को मैनेज करने वाले फ़ंक्शन लिखने के बारे में जानकारी पाने के लिए, अपने एक्सटेंशन के लाइफ़साइकल इवेंट मैनेज करना लेख पढ़ें.

टास्क की कतार वाले फ़ंक्शन लिखने के बारे में जानकारी पाने के लिए, Cloud Functions के दस्तावेज़ में Cloud Tasks की मदद से फ़ंक्शन को कतार में लगाएं लेख पढ़ें.

फ़ंक्शन की परिभाषा (सिर्फ़ पहली जनरेशन के लिए)

import { tasks } from "firebase-functions/v1";

export const yourFunctionName = tasks.taskQueue().onDispatch(async (data, context) => {
  // ...
});

संसाधन का एलान (extension.yaml)

resources:
  - name: myTaskFunction
    type: firebaseextensions.v1beta.function
    description: >-
      Perform a task when triggered by a lifecycle event
    properties:
      taskQueueTrigger: {}

taskQueueTrigger प्रॉपर्टी सेट को {} पर सेट करें. इसके अलावा, विकल्पों का ऐसा मैप भी इस्तेमाल किया जा सकता है जो टास्क की कतार की दर की सीमाओं और फिर से कोशिश करने के तरीके को बेहतर बनाता है. इसके बारे में जानने के लिए, टास्क की कतार को बेहतर बनाना लेख पढ़ें.

अगर आपको एक्सटेंशन के लाइफ़साइकल इवेंट पर फ़ंक्शन को ट्रिगर करना है, तो फ़ंक्शन के नाम और प्रोसेस करने से जुड़ा एक मैसेज (यह ज़रूरी नहीं है) के साथ lifecycleEvents रिकॉर्ड जोड़ें. प्रोसेस करने से जुड़ा यह मैसेज, प्रोसेस शुरू होने पर Firebase कंसोल में दिखेगा.

lifecycleEvents:
  onInstall:
    function: myTaskFunction
    processingMessage: Resizing your existing images
  onUpdate:
    function: myOtherTaskFunction
    processingMessage: Setting up your extension
  onConfigure:
    function: myOtherTaskFunction
    processingMessage: Setting up your extension

Analytics

Analytics से ट्रिगर होने वाला फ़ंक्शन तब काम करता है, जब Analytics का कोई तय इवेंट लॉग हो जाता है.

Analytics से ट्रिगर होने वाले फ़ंक्शन लिखने के बारे में जानकारी पाने के लिए, Cloud Functions के दस्तावेज़ में Google Analytics ट्रिगर देखें.

फ़ंक्शन की परिभाषा (सिर्फ़ पहली जनरेशन के लिए)

import { analytics } from "firebase-functions/v1";

export const yourFunctionName = analytics.event("event_name").onLog((event, context) => {
  // ...
});

संसाधन का एलान (extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: providers/google.firebase.analytics/eventTypes/event.log
        resource: projects/${PROJECT_ID}/events/ga_event
  - name: anotherFunction
    type: ...

अगर आपको उपयोगकर्ताओं को यह सुविधा देनी है कि वे एक्सटेंशन इंस्टॉल करते समय, Analytics इवेंट को सुनने के लिए कॉन्फ़िगर कर सकें, तो अपनी extension.yaml फ़ाइल में एक नया पैरामीटर जोड़ें. साथ ही, अपने फ़ंक्शन के resource एलान में पैरामीटर का रेफ़रंस दें:

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: providers/google.firebase.analytics/eventTypes/event.log
        resource: projects/${PROJECT_ID}/events/${EVENT_NAME}
  - name: anotherFunction
    type: ...

params:
  - param: EVENT_NAME
    label: Analytics event
    description: What event do you want to respond to?
    type: string
    default: ga_event  # Specifying a default is optional.
    required: true

Authentication

पुष्टि करने की सुविधा से ट्रिगर होने वाला फ़ंक्शन, तब चलता है, जब किसी उपयोगकर्ता का खाता बनाया या मिटाया जाता है.

पुष्टि करने की सुविधा से ट्रिगर होने वाले फ़ंक्शन लिखने के बारे में जानकारी पाने के लिए, Cloud Functions दस्तावेज़ में Firebase Authentication ट्रिगर देखें.

फ़ंक्शन की परिभाषा (सिर्फ़ पहली जनरेशन के लिए)

import { auth } from "firebase-functions/v1";

export const yourFunctionName = auth.user().onCreate((user, context) => {
  // ...
});

export const yourFunctionName2 = auth.user().onDelete((user, context) => {
  // ...
});

संसाधन का एलान (extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: providers/firebase.auth/eventTypes/user.create
        resource: projects/${PROJECT_ID}
  - name: anotherFunction
    type: ...

यहां दी गई टेबल में, हर Authentication इवेंट टाइप को तय करने का तरीका बताया गया है:

Cloud Functions इवेंट ट्रिगर eventType ब्यौरा
onCreate() providers/firebase.auth/eventTypes/user.create नया उपयोगकर्ता बनाया गया
onDelete() providers/firebase.auth/eventTypes/user.delete उपयोगकर्ता का खाता मिटाया गया

Cloud Firestore

Cloud Firestore से ट्रिगर होने वाला फ़ंक्शन तब चलता है, जब कोई दस्तावेज़ बनाया जाता है, अपडेट किया जाता है या मिटाया जाता है.

Firestore से ट्रिगर होने वाले फ़ंक्शन लिखने के बारे में जानकारी पाने के लिए, Cloud Functions के दस्तावेज़ में Cloud Firestore ट्रिगर देखें.

फ़ंक्शन की परिभाषा (सिर्फ़ पहली जनरेशन के लिए)

import { firestore } from "firebase-functions/v1";

export const yourFunctionName = firestore.document("collection/{doc_id}")
  .onCreate((snapshot, context) => {
    // ...
  });

export const yourFunctionName2 = firestore.document("collection/{doc_id}")
  .onUpdate((change, context) => {
    // ...
  });

export const yourFunctionName3 = firestore.document("collection/{doc_id}")
  .onDelete((snapshot, context) => {
    // ...
  });

export const yourFunctionName4 = firestore.document("collection/{doc_id}")
  .onWrite((change, context) => {
    // onWrite triggers on creation, update, and deletion.
    // ...
  });

संसाधन का एलान (extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: providers/cloud.firestore/eventTypes/document.write
        resource: projects/${PROJECT_ID}/databases/(default)/documents/collection/{documentID}
  - name: anotherFunction
    type: ...

यहां दी गई टेबल में, हर Cloud Firestoreइवेंट टाइप को तय करने का तरीका बताया गया है:

Cloud Functions इवेंट ट्रिगर eventType ब्यौरा
onCreate() providers/cloud.firestore/eventTypes/document.create नया दस्तावेज़ बनाया गया
onDelete() providers/cloud.firestore/eventTypes/document.delete दस्तावेज़ मिटाया गया
onUpdate() providers/cloud.firestore/eventTypes/document.update दस्तावेज़ अपडेट किया गया
onWrite() providers/cloud.firestore/eventTypes/document.write दस्तावेज़ बनाया गया, मिटाया गया या अपडेट किया गया

अगर आपको उपयोगकर्ताओं को एक्सटेंशन इंस्टॉल करते समय, दस्तावेज़ का पाथ कॉन्फ़िगर करने की सुविधा देनी है, तो अपनी extension.yaml फ़ाइल में एक नया पैरामीटर जोड़ें. साथ ही, अपने फ़ंक्शन के resource डिक्लेरेशन में पैरामीटर का रेफ़रंस दें:

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: providers/cloud.firestore/eventTypes/document.write
        resource: projects/${PROJECT_ID}/databases/(default)/documents/${YOUR_DOCUMENT_PATH}
  - name: anotherFunction
    type: ...

params:
  - param: YOUR_DOCUMENT_PATH
    label: Cloud Firestore path
    description: Where do you want to watch for changes?
    type: string
    default: path/to/{documentID}  # Specifying a default is optional.
    required: true

Pub/Sub

Pub/Sub से ट्रिगर होने वाला फ़ंक्शन तब चलता है, जब किसी खास विषय पर कोई मैसेज पब्लिश किया जाता है.

Pub/Sub से ट्रिगर होने वाले फ़ंक्शन लिखने के बारे में जानकारी पाने के लिए, Cloud Functions के दस्तावेज़ में Pub/Sub ट्रिगर देखें.

फ़ंक्शन की परिभाषा (सिर्फ़ पहली जनरेशन के लिए)

import { pubsub } from "firebase-functions/v1";

export const yourFunctionName = pubsub.topic("topic_name").onPublish((message, context) => {
  // ...
});

संसाधन का एलान (extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: google.pubsub.topic.publish
        resource: projects/${PROJECT_ID}/topics/topic-name
  - name: anotherFunction
    type: ...

अगर आपको उपयोगकर्ताओं को एक्सटेंशन इंस्टॉल करते समय Pub/Sub विषय को कॉन्फ़िगर करने की सुविधा देनी है, तो अपनी extension.yaml फ़ाइल में एक नया पैरामीटर जोड़ें. साथ ही, अपने फ़ंक्शन के resource एलान में पैरामीटर का रेफ़रंस दें:

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: google.pubsub.topic.publish
        resource: projects/${PROJECT_ID}/topics/${PUBSUB_TOPIC}
  - name: anotherFunction
    type: ...

params:
  - param: PUBSUB_TOPIC
    label: Pub/Sub topic
    description: Which Pub/Sub topic do you want to watch for messages?
    type: string
    default: topic-name  # Specifying a default is optional.
    required: true

Realtime Database

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

आरटीडीबी ट्रिगर किए गए फ़ंक्शन लिखने के बारे में जानकारी पाने के लिए, Cloud Functions के दस्तावेज़ में Realtime Database ट्रिगर देखें.

फ़ंक्शन की परिभाषा (सिर्फ़ पहली जनरेशन के लिए)

import { database } from "firebase-functions/v1";

export const yourFunctionName = database.ref("path/to/{item}")
  .onCreate((snapshot, context) => {
    // ...
  });

export const yourFunctionName2 = database.ref("path/to/{item}")
  .onUpdate((change, context) => {
    // ...
  });

export const yourFunctionName3 = database.ref("path/to/{item}")
  .onDelete((snapshot, context) => {
    // ...
  });

export const yourFunctionName4 = database.ref("path/to/{item}")
  .onWrite((change, context) => {
    // onWrite triggers on creation, update, and deletion.
    // ...
  });

संसाधन का एलान (extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: providers/google.firebase.database/eventTypes/ref.create
        # DATABASE_INSTANCE (project's default instance) is an auto-populated
        # parameter value. You can also specify an instance.
        resource: projects/_/instances/${DATABASE_INSTANCE}/refs/path/to/{itemId}
  - name: anotherFunction
    type: ...

यहां दी गई टेबल में, हर Cloud Firestoreइवेंट टाइप को तय करने का तरीका बताया गया है:

Cloud Functions इवेंट ट्रिगर eventType ब्यौरा
onCreate() providers/google.firebase.database/eventTypes/ref.create डेटा बनाया गया
onDelete() providers/google.firebase.database/eventTypes/ref.delete डेटा मिटाया गया
onUpdate() providers/google.firebase.database/eventTypes/ref.update डेटा अपडेट किया गया
onWrite() providers/google.firebase.database/eventTypes/ref.write डेटा बनाया गया, मिटाया गया या अपडेट किया गया

अगर आपको उपयोगकर्ताओं को एक्सटेंशन इंस्टॉल करते समय, देखने के लिए पाथ कॉन्फ़िगर करने की सुविधा देनी है, तो अपनी extension.yaml फ़ाइल में एक नया पैरामीटर जोड़ें. साथ ही, अपने फ़ंक्शन के resource डेक्लेरेशन में पैरामीटर का रेफ़रंस दें:

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: providers/google.firebase.database/eventTypes/ref.create
        # DATABASE_INSTANCE (project's default instance) is an auto-populated
        # parameter value. You can also specify an instance.
        resource: projects/_/instances/${DATABASE_INSTANCE}/refs/${DB_PATH}
  - name: anotherFunction
    type: ...

params:
  - param: DB_PATH
    label: Realtime Database path
    description: Where do you want to watch for changes?
    type: string
    default: path/to/{itemId}  # Specifying a default is optional.
    required: true

Remote Config

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

रिमोट कॉन्फ़िगरेशन से ट्रिगर होने वाले फ़ंक्शन लिखने के बारे में जानकारी पाने के लिए, Cloud Functions के दस्तावेज़ में रिमोट कॉन्फ़िगरेशन ट्रिगर देखें.

फ़ंक्शन की परिभाषा (सिर्फ़ पहली जनरेशन के लिए)

import { remoteConfig } from "firebase-functions/v1";

export const yourFunctionName = remoteConfig.onUpdate((version, context) => {
  // ...
});

संसाधन का एलान (extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: google.firebase.remoteconfig.update
        resource: projects/${PROJECT_ID}
  - name: anotherFunction
    type: ...

Cloud Storage

Cloud Storage से ट्रिगर होने वाला फ़ंक्शन तब चलता है, जब कोई ऑब्जेक्ट बनाया जाता है, संग्रहित किया जाता है या मिटाया जाता है. इसके अलावा, यह तब भी चलता है, जब उसके मेटाडेटा में बदलाव होता है.

Storage से ट्रिगर होने वाले फ़ंक्शन लिखने के बारे में जानकारी पाने के लिए, Cloud Functions के दस्तावेज़ में Cloud Storage ट्रिगर देखें.

फ़ंक्शन की परिभाषा (सिर्फ़ पहली जनरेशन के लिए)

import { storage } from "firebase-functions/v1";

export const yourFunctionName = storage.object().onFinalize((object, context) => {
  // ...
});

export const yourFunctionName2 = storage.object().onMetadataUpdate((object, context) => {
  // ...
});

export const yourFunctionName3 = storage.object().onArchive((object, context) => {
  // ...
});

export const yourFunctionName4 = storage.object().onDelete((object, context) => {
  // ...
});

संसाधन का एलान (extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: google.storage.object.finalize
        # STORAGE_BUCKET (project's default bucket) is an auto-populated
        # parameter. You can also specify a bucket.
        resource: projects/_/buckets/${STORAGE_BUCKET}
  - name: anotherFunction
    type: ...

यहां दी गई टेबल में, हर Cloud Storageइवेंट टाइप को तय करने का तरीका बताया गया है:

Cloud Functions इवेंट ट्रिगर eventType ब्यौरा
onFinalize() google.storage.object.finalize ऑब्जेक्ट बनाया गया
onMetadataUpdate() google.storage.object.metadataUpdate ऑब्जेक्ट का मेटाडेटा अपडेट किया गया
onArchive() google.storage.object.archive ऑब्जेक्ट को संग्रहित किया गया
onDelete() google.storage.object.delete ऑब्जेक्ट मिटाया गया

अगर आपको उपयोगकर्ताओं को एक्सटेंशन इंस्टॉल करते समय स्टोरेज बकेट कॉन्फ़िगर करने की सुविधा देनी है, तो अपनी extension.yaml फ़ाइल में एक नया पैरामीटर जोड़ें. साथ ही, अपने फ़ंक्शन के resource डिक्लेरेशन में पैरामीटर का रेफ़रंस दें:

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: google.storage.object.finalize
        resource: projects/_/buckets/${YOUR_BUCKET}
  - name: anotherFunction
    type: ...

params:
  - param: YOUR_BUCKET
    label: Cloud Storage bucket
    description: Which bucket do you want to watch for changes?
    type: selectResource
    resourceType: storage.googleapis.com/Bucket
    default: ${STORAGE_BUCKET}  # Specifying a default is optional.
    required: true

Test Lab

Test Lab से ट्रिगर होने वाला फ़ंक्शन तब काम करता है, जब टेस्ट मैट्रिक्स अपने टेस्ट पूरे कर लेता है.

टेस्ट लैब से ट्रिगर होने वाले फ़ंक्शन लिखने के बारे में जानकारी पाने के लिए, Cloud Functions दस्तावेज़ में Firebase टेस्ट लैब ट्रिगर देखें.

फ़ंक्शन की परिभाषा (सिर्फ़ पहली जनरेशन के लिए)

import { testLab } from "firebase-functions/v1";

export const yourFunctionName = testLab.testMatrix().onComplete((matrix, context) => {
  // ...
});

संसाधन का एलान (extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: google.testing.testMatrix.complete
        resource: projects/${PROJECT_ID}/testMatrices/{matrixId}
  - name: anotherFunction
    type: ...

Crashlytics सूचनाएं ट्रिगर करने की सुविधा

Crashlytics-ट्रिगर किया गया फ़ंक्शन तब चलता है, जब Crashlytics कोई सूचना पब्लिश करता है.

सूचना ट्रिगर करने वाले फ़ंक्शन लिखने के बारे में जानकारी पाने के लिए, Cloud Functions के दस्तावेज़ में Firebase सूचनाएं ट्रिगर करने की सुविधा देखें.

फ़ंक्शन की परिभाषा (सिर्फ़ दूसरी जनरेशन के लिए)

import {
  onNewFatalIssuePublished,
  onNewNonfatalIssuePublished,
  onNewAnrIssuePublished,
  onRegressionAlertPublished,
  onVelocityAlertPublished,
  onStabilityDigestPublished,
} from "firebase-functions/v2/alerts/crashlytics";

export const yourFunctionName = onNewFatalIssuePublished((event) => {
  // ...
});

export const yourFunctionName2 = onNewNonfatalIssuePublished((event) => {
  // ...
});

export const yourFunctionName3 = onNewAnrIssuePublished((event) => {
  // ...
});

export const yourFunctionName4 = onRegressionAlertPublished((event) => {
  // ...
});

export const yourFunctionName5 = onVelocityAlertPublished((event) => {
  // ...
});

export const yourFunctionName6 = onStabilityDigestPublished((event) => {
  // ...
});

संसाधन का एलान (extension.yaml)

apis:
  - apiName: eventarc.googleapis.com
    reason: Powers all events and triggers
  - apiName: run.googleapis.com
    reason: Powers 2nd-gen functions

resources:
  - name: yourfunctionname
    type: firebaseextensions.v1beta.v2function
    properties:
      buildConfig:
        runtime: nodejs16
      serviceConfig:
        availableMemory: 512M
      eventTrigger:
        eventType: google.firebase.firebasealerts.alerts.v1.published
        triggerRegion: global
        eventFilters:
          - attribute: alerttype
            value: crashlytics.newFatalIssue
  - name: anotherFunction
    type: ...

alerttype के लिए, इन वैल्यू का इस्तेमाल किया जा सकता है

  • crashlytics.newFatalIssue
  • crashlytics.newNonfatalIssue
  • crashlytics.regression
  • crashlytics.stabilityDigest
  • crashlytics.velocity
  • crashlytics.newAnrIssue

Performance Monitoring सूचनाएं ट्रिगर करने की सुविधा

Performance Monitoring-ट्रिगर किया गया फ़ंक्शन तब चलता है, जब Performance Monitoring कोई सूचना पब्लिश करता है.

सूचना ट्रिगर करने वाले फ़ंक्शन लिखने के बारे में जानकारी पाने के लिए, Cloud Functions के दस्तावेज़ में Firebase सूचनाएं ट्रिगर करने की सुविधा देखें.

फ़ंक्शन की परिभाषा (सिर्फ़ दूसरी जनरेशन के लिए)

import { onThresholdAlertPublished } from "firebase-functions/v2/alerts/performance";

export const yourFunctionName = onThresholdAlertPublished((event) => {
  // ...
});

संसाधन का एलान (extension.yaml)

apis:
  - apiName: eventarc.googleapis.com
    reason: Powers all events and triggers
  - apiName: run.googleapis.com
    reason: Powers 2nd-gen functions

resources:
  - name: yourfunctionname
    type: firebaseextensions.v1beta.v2function
    properties:
      buildConfig:
        runtime: nodejs16
      serviceConfig:
        availableMemory: 512M
      eventTrigger:
        eventType: google.firebase.firebasealerts.alerts.v1.published
        triggerRegion: global
        eventFilters:
          - attribute: alerttype
            value: performance.threshold
  - name: anotherFunction
    type: ...

App Distribution सूचनाएं ट्रिगर करने की सुविधा

App Distribution-ट्रिगर किया गया फ़ंक्शन तब चलता है, जब App Distribution कोई सूचना पब्लिश करता है.

सूचना ट्रिगर करने वाले फ़ंक्शन लिखने के बारे में जानकारी पाने के लिए, Cloud Functions के दस्तावेज़ में Firebase सूचनाएं ट्रिगर करने की सुविधा देखें.

फ़ंक्शन की परिभाषा (सिर्फ़ दूसरी जनरेशन के लिए)

import {
  onNewTesterIosDevicePublished,
  onInAppFeedbackPublished
} from "firebase-functions/v2/alerts/appDistribution";

export const yourFunctionName = onNewTesterIosDevicePublished((event) => {
  // ...
});

export const yourFunctionName2 = onInAppFeedbackPublished((event) => {
  // ...
});

संसाधन का एलान (extension.yaml)

apis:
  - apiName: eventarc.googleapis.com
    reason: Powers all events and triggers
  - apiName: run.googleapis.com
    reason: Powers 2nd-gen functions

resources:
  - name: yourfunctionname
    type: firebaseextensions.v1beta.v2function
    properties:
      buildConfig:
        runtime: nodejs16
      serviceConfig:
        availableMemory: 512M
      eventTrigger:
        eventType: google.firebase.firebasealerts.alerts.v1.published
        triggerRegion: global
        eventFilters:
          - attribute: alerttype
            value: appDistribution.inAppFeedback
  - name: anotherFunction
    type: ...

alerttype के लिए, इन वैल्यू का इस्तेमाल किया जा सकता है

  • appDistribution.newTesterIosDevice
  • appDistribution.inAppFeedback

कस्टम इवेंट ट्रिगर (Eventarc)

Eventarc से ट्रिगर होने वाला फ़ंक्शन तब चलता है, जब किसी चैनल पर कोई खास इवेंट टाइप पब्लिश किया जाता है.

Eventarc ट्रिगर किए गए फ़ंक्शन लिखने के बारे में जानकारी के लिए, Cloud Functions दस्तावेज़ में कस्टम इवेंट ट्रिगर बनाना और उन्हें मैनेज करना लेख पढ़ें.

आपके पास अपने एक्सटेंशन से इवेंट पब्लिश करने का विकल्प भी होता है. इससे उपयोगकर्ताओं को अपने एक्सटेंशन में कस्टम लॉजिक डालने का तरीका मिलता है. एक्सटेंशन में डेवलपर के दिए गए कस्टम लॉजिक का इस्तेमाल करना लेख पढ़ें.

फ़ंक्शन की परिभाषा (सिर्फ़ दूसरी जनरेशन के लिए)

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

export const yourFunctionName = onCustomEventPublished((event) => {
  // ...
});

संसाधन का एलान (extension.yaml)

apis:
  - apiName: eventarc.googleapis.com
    reason: Powers all events and triggers
  - apiName: run.googleapis.com
    reason: Powers 2nd-gen functions

resources:
  - name: yourfunctionname
    type: firebaseextensions.v1beta.v2function
    properties:
      # LOCATION is a user-configured parameter value specified by the user
      # during installation.
      location: ${param:LOCATION}
      buildConfig:
        runtime: nodejs16
      serviceConfig:
        availableMemory: 512M
        timeoutSeconds: 60
      eventTrigger:
        eventType: firebase.extensions.storage-resize-images.v1.complete
        channel: projects/${param:PROJECT_ID}/locations/us-central1/channels/firebase
  - name: anotherFunction
    type: ...

एक्सटेंशन इंस्टॉल करते समय, चैनल पहले से मौजूद होना चाहिए. उदाहरण के लिए, अगर आपको चैनल बनाने वाले किसी दूसरे एक्सटेंशन के कस्टम इवेंट पर निर्भर रहना है, तो अपने उपयोगकर्ताओं को उस एक्सटेंशन को पहले इंस्टॉल करने के लिए कहें.

ऊपर दिए गए उदाहरण से, us-central1 क्षेत्र में "default" Firebase चैनल के लिए कस्टम इवेंट ट्रिगर बनाया जाएगा. पैरामीटर का इस्तेमाल करके, चैनल के नाम और क्षेत्र को पसंद के मुताबिक बनाया जा सकता है. उदाहरण के लिए:


params:
  - param: EVENTARC_CHANNEL_NAME
    label: Eventarc channel name
    description: What is the name of the Eventarc channel.
    default: firebase
    type: string
    required: true

resources:
  - name: yourfunctionname
    type: firebaseextensions.v1beta.v2function
    properties:
      location: ${param:LOCATION}
      eventTrigger:
        eventType: firebase.extensions.storage-resize-images.v1.complete
        channel: projects/${param:PROJECT_ID}/locations/${param:LOCATION}/channels/${param:EVENTARC_CHANNEL_NAME}