Google Cloud का Pub/Sub, दुनिया भर में डिस्ट्रिब्यूट किया गया मैसेज बस है. यह आपकी ज़रूरत के हिसाब से अपने-आप स्केल होता है. किसी खास विषय पर नया Pub/Sub मैसेज भेजे जाने पर, कोई फ़ंक्शन ट्रिगर किया जा सकता है.
ज़रूरी मॉड्यूल इंपोर्ट करना
शुरू करने के लिए, Pub/Sub इवेंट को मैनेज करने के लिए ज़रूरी मॉड्यूल इंपोर्ट करें:
Node.js
const {onMessagePublished} = require("firebase-functions/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"]