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

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

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

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

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

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

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

  • फ़ंक्शन की कुछ परिभाषाओं के लिए, आपको ऐसी जानकारी देनी होगी जो 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 कमांड की मदद से, फ़ंक्शन लिखने के लिए 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 फ़ाइल में एक नया पैरामीटर जोड़ें. साथ ही, अपने फ़ंक्शन के 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 से पुष्टि करने की प्रोसेस के ट्रिगर देखें.

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

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 दस्तावेज़ में रीयलटाइम डेटाबेस ट्रिगर देखें.

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

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 से ट्रिगर होने वाला फ़ंक्शन तब चलता है, जब कोई ऑब्जेक्ट बनाया जाता है, संग्रहित किया जाता है या मिटाया जाता है. इसके अलावा, यह फ़ंक्शन तब भी चलता है, जब ऑब्जेक्ट के मेटाडेटा में बदलाव होता है.

स्टोरेज से ट्रिगर होने वाले फ़ंक्शन लिखने के बारे में जानकारी पाने के लिए, 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)

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)

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)

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)

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 क्षेत्र में "डिफ़ॉल्ट" 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}