Pub/Sub ट्रिगर

Google Cloud का Pub/Sub, दुनिया भर में इस्तेमाल किया जाने वाला मैसेज बस है. यह आपकी ज़रूरत के हिसाब से अपने-आप स्केल हो जाता है. किसी खास विषय पर नया Pub/Sub मैसेज भेजे जाने पर, फ़ंक्शन को ट्रिगर किया जा सकता है.

ज़रूरी मॉड्यूल इंपोर्ट करना

शुरू करने के लिए, Pub/Sub इवेंट मैनेज करने के लिए ज़रूरी मॉड्यूल इंपोर्ट करें:

Node.js

const {onMessagePublished} = require("firebase-functions/v2/pubsub");
const logger = require("firebase-functions/logger");

Python

from firebase_functions import pubsub_fn

फ़ंक्शन को ट्रिगर करना

आपको उस Pub/Sub विषय का नाम बताना होगा जिसके लिए आपको फ़ंक्शन ट्रिगर करना है. साथ ही, इवेंट हैंडलर में इवेंट सेट करना होगा:

Node.js

exports.hellopubsub = onMessagePublished("topic-name", (event) => {

Python

@pubsub_fn.on_message_published(topic="topic-name")
def hellopubsub(event: pubsub_fn.CloudEvent[pubsub_fn.MessagePublishedData]) -> None:
    """Log a message using data published to a Pub/Sub topic."""

Pub/Sub मैसेज के पेलोड को ऐक्सेस करना

Pub/Sub मैसेज का पेलोड, आपके फ़ंक्शन को मिले मैसेज ऑब्जेक्ट से ऐक्सेस किया जा सकता है. Pub/Sub मैसेज के मुख्य हिस्से में JSON वाले मैसेज के लिए, Cloud Functions के Firebase SDK टूल में मैसेज को डिकोड करने के लिए हेल्पर प्रॉपर्टी होती है. उदाहरण के लिए, यहां एक सामान्य JSON पेलोड के साथ पब्लिश किया गया मैसेज दिया गया है:

gcloud pubsub topics publish topic-name --message '{"name":"Xenia"}'

json प्रॉपर्टी के ज़रिए, इस तरह के JSON डेटा पेलोड को ऐक्सेस किया जा सकता है:

Node.js

  // Get the `name` attribute of the PubSub message JSON body.
  let name = null;
  try {
    name = event.data.message.json.name;
  } catch (e) {
    logger.error("PubSub message was not JSON", e);
  }

Python

# Get the `name` attribute of the PubSub message JSON body.
try:
    data = event.data.message.json
except ValueError:
    print("PubSub message was not JSON")
    return
if data is None:
    return
if "name" not in data:
    print("No 'name' key")
    return
name = data["name"]

अन्य, नॉन-JSON पेलोड को Pub/Sub मैसेज में शामिल किया जाता है. इन्हें मैसेज ऑब्जेक्ट में base64 एन्कोड की गई स्ट्रिंग के तौर पर शामिल किया जाता है. नीचे दिए गए मैसेज को पढ़ने के लिए, आपको base64 एन्कोड की गई स्ट्रिंग को डिकोड करना होगा. इसके लिए, यहां दिया गया तरीका अपनाएं:

gcloud pubsub topics publish topic-name --message 'MyMessage'

Node.js

// Decode the PubSub Message body.
const message = event.data.message;
const messageBody = message.data ?
      Buffer.from(message.data, "base64").toString() :
      null;

Python

# Decode the PubSub message body.
message_body = base64.b64decode(event.data.message.data)

मैसेज के एट्रिब्यूट ऐक्सेस करना

Pub/Sub मैसेज को पब्लिश कमांड में सेट किए गए डेटा एट्रिब्यूट के साथ भेजा जा सकता है. उदाहरण के लिए, name एट्रिब्यूट के साथ कोई मैसेज पब्लिश किया जा सकता है:

gcloud pubsub topics publish topic-name --attribute name=Xenia

इन एट्रिब्यूट को मैसेज ऑब्जेक्ट की प्रॉपर्टी से पढ़ा जा सकता है:

Node.js

// Get the `name` attribute of the message.
const name = event.data.message.attributes.name;

Python

# Get the `name` attribute of the message.
if "name" not in event.data.message.attributes:
    print("No 'name' attribute")
    return
name = event.data.message.attributes["name"]