Google Cloud का Pub/Sub, दुनिया भर में डिस्ट्रिब्यूट किया जाने वाला मैसेज बस है. यह आपकी ज़रूरत के हिसाब से अपने-आप स्केल हो जाता है. जब भी किसी खास विषय पर नया Pub/Sub मैसेज भेजा जाता है, तो आपके पास कोई फ़ंक्शन ट्रिगर करने का विकल्प होता है.
ज़रूरी मॉड्यूल इंपोर्ट करना
शुरू करने के लिए, Pub/Sub इवेंट मैनेज करने के लिए ज़रूरी मॉड्यूल इंपोर्ट करें:
const {onMessagePublished} = require("firebase-functions/v2/pubsub");
const logger = require("firebase-functions/logger");
from firebase_functions import pubsub_fn
फ़ंक्शन को ट्रिगर करना
आपको वह Pub/Sub विषय का नाम बताना होगा जिससे आपको अपना फ़ंक्शन ट्रिगर करना है. साथ ही, इवेंट हैंडलर में इवेंट सेट करना होगा:
exports.hellopubsub = onMessagePublished("topic-name", (event) => {
@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 डेटा पेलोड को इस तरह ऐक्सेस किया जा सकता है:
// 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); }
# 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'
// Decode the PubSub Message body. const message = event.data.message; const messageBody = message.data ? Buffer.from(message.data, "base64").toString() : null;
# 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
इस तरह के एट्रिब्यूट को मैसेज ऑब्जेक्ट की संबंधित प्रॉपर्टी से पढ़ा जा सकता है:
// Get the `name` attribute of the message. const name = event.data.message.attributes.name;
# 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"]