সেভ করা পৃষ্ঠা গুছিয়ে রাখতে 'সংগ্রহ' ব্যবহার করুন
আপনার পছন্দ অনুযায়ী কন্টেন্ট সেভ করুন ও সঠিক বিভাগে রাখুন।
Google CloudPub/Sub একটি বিশ্বব্যাপী বিতরণ করা বার্তা বাস যা আপনার প্রয়োজন অনুযায়ী স্বয়ংক্রিয়ভাবে স্কেল করে। যখনই একটি নির্দিষ্ট বিষয়ে একটি নতুন Pub/Sub বার্তা পাঠানো হয় আপনি একটি ফাংশন ট্রিগার করতে পারেন৷
প্রয়োজনীয় মডিউল আমদানি করুন
শুরু করতে, Pub/Sub ইভেন্টগুলি পরিচালনা করার জন্য প্রয়োজনীয় মডিউলগুলি আমদানি করুন:
@pubsub_fn.on_message_published(topic="topic-name")defhellopubsub(event:pubsub_fn.CloudEvent[pubsub_fn.MessagePublishedData])-> None:"""Log a message using data published to a Pub/Sub topic."""
Pub/Sub বার্তার জন্য পেলোড আপনার ফাংশনে ফিরে আসা বার্তা বস্তু থেকে অ্যাক্সেসযোগ্য। Pub/Sub মেসেজ বডিতে JSON-এর সাথে মেসেজের জন্য, Cloud Functions জন্য Firebase SDK-এ মেসেজ ডিকোড করার জন্য একটি সহায়ক প্রপার্টি রয়েছে। উদাহরণস্বরূপ, এখানে একটি সাধারণ JSON পেলোড সহ প্রকাশিত একটি বার্তা রয়েছে:
আপনি json সম্পত্তির মাধ্যমে এই মত একটি JSON ডেটা পেলোড অ্যাক্সেস করতে পারেন:
Node.js
// Get the `name` attribute of the PubSub message JSON body.letname=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.jsonexceptValueError:print("PubSub message was not JSON")returnifdataisNone:returnif"name"notindata:print("No 'name' key")returnname=data["name"]
অন্যান্য, নন-JSON পেলোডগুলি বার্তা অবজেক্টে বেস64 এনকোড করা স্ট্রিং হিসাবে Pub/Sub বার্তায় থাকে। নিম্নলিখিত মত একটি বার্তা পড়তে, আপনি দেখানো হিসাবে base64 এনকোডেড স্ট্রিং ডিকোড করতে হবে:
# Get the `name` attribute of the message.if"name"notinevent.data.message.attributes:print("No 'name' attribute")returnname=event.data.message.attributes["name"]
[[["সহজে বোঝা যায়","easyToUnderstand","thumb-up"],["আমার সমস্যার সমাধান হয়েছে","solvedMyProblem","thumb-up"],["অন্যান্য","otherUp","thumb-up"]],[["এতে আমার প্রয়োজনীয় তথ্য নেই","missingTheInformationINeed","thumb-down"],["খুব জটিল / অনেক ধাপ","tooComplicatedTooManySteps","thumb-down"],["পুরনো","outOfDate","thumb-down"],["অনুবাদ সংক্রান্ত সমস্যা","translationIssue","thumb-down"],["নমুনা / কোড সংক্রান্ত সমস্যা","samplesCodeIssue","thumb-down"],["অন্যান্য","otherDown","thumb-down"]],["2025-09-06 UTC-তে শেষবার আপডেট করা হয়েছে।"],[],[],null,["\u003cbr /\u003e\n\nGoogle Cloud's [Pub/Sub](https://cloud.google.com/pubsub/docs/) is a\nglobally distributed message bus that automatically scales as you need it.\nYou can trigger a function whenever a new Pub/Sub message is sent\nto a specific topic.\n\nImport the required modules\n\nTo get started, import the modules required for handling Pub/Sub\nevents: \n\nNode.js \n\n const {onMessagePublished} = require(\"firebase-functions/v2/pubsub\");\n const logger = require(\"firebase-functions/logger\"); \n https://github.com/firebase/functions-samples/blob/c4fde45b65fab584715e786ce3264a6932d996ec/Node/quickstarts/pubsub-helloworld/functions/index.js#L19-L20\n\nPython \n\n from firebase_functions import pubsub_fn \n https://github.com/firebase/functions-samples/blob/c4fde45b65fab584715e786ce3264a6932d996ec/Python/quickstarts/pubsub-helloworld/functions/main.py#L18-L18\n\nTrigger the function\n\nYou must specify the Pub/Sub topic name that\nyou want to trigger your function, and set the event within the\nevent handler: \n\nNode.js \n\n exports.hellopubsub = onMessagePublished(\"topic-name\", (event) =\u003e { \n https://github.com/firebase/functions-samples/blob/c4fde45b65fab584715e786ce3264a6932d996ec/Node/quickstarts/pubsub-helloworld/functions/index.js#L29-L29\n\nPython \n\n @pubsub_fn.on_message_published(topic=\"topic-name\")\n def hellopubsub(event: pubsub_fn.CloudEvent[pubsub_fn.MessagePublishedData]) -\u003e None:\n \"\"\"Log a message using data published to a Pub/Sub topic.\"\"\" \n https://github.com/firebase/functions-samples/blob/c4fde45b65fab584715e786ce3264a6932d996ec/Python/quickstarts/pubsub-helloworld/functions/main.py#L24-L26\n\nAccess the pub/sub message payload\n\nThe payload for the Pub/Sub message is accessible from the\nmessage object returned\nto your function. For messages with JSON in the Pub/Sub message\nbody, the Firebase SDK for Cloud Functions has a helper property to decode the message. For\nexample, here is a message published with a simple JSON payload: \n\n gcloud pubsub topics publish topic-name --message '{\"name\":\"Xenia\"}'\n\nYou can access a JSON data payload like this via the\n`json` property: \n\nNode.js \n\n```javascript\n // Get the `name` attribute of the PubSub message JSON body.\n let name = null;\n try {\n name = event.data.message.json.name;\n } catch (e) {\n logger.error(\"PubSub message was not JSON\", e);\n }https://github.com/firebase/functions-samples/blob/c4fde45b65fab584715e786ce3264a6932d996ec/Node/quickstarts/pubsub-helloworld/functions/index.js#L51-L57\n```\n\nPython \n\n # Get the `name` attribute of the PubSub message JSON body.\n try:\n data = event.data.message.json\n except ValueError:\n print(\"PubSub message was not JSON\")\n return\n if data is None:\n return\n if \"name\" not in data:\n print(\"No 'name' key\")\n return\n name = data[\"name\"] \n https://github.com/firebase/functions-samples/blob/c4fde45b65fab584715e786ce3264a6932d996ec/Python/quickstarts/pubsub-helloworld/functions/main.py#L42-L53\n\nOther, non-JSON payloads are contained in the Pub/Sub message as\nbase64 encoded strings in the message object. To read a message like the\nfollowing, you must decode the base64 encoded string as shown: \n\n gcloud pubsub topics publish topic-name --message 'MyMessage'\n\nNode.js \n\n```javascript\n// Decode the PubSub Message body.\nconst message = event.data.message;\nconst messageBody = message.data ?\n Buffer.from(message.data, \"base64\").toString() :\n null;https://github.com/firebase/functions-samples/blob/c4fde45b65fab584715e786ce3264a6932d996ec/Node/quickstarts/pubsub-helloworld/functions/index.js#L32-L36\n```\n\nPython \n\n # Decode the PubSub message body.\n message_body = base64.b64decode(event.data.message.data) \n https://github.com/firebase/functions-samples/blob/c4fde45b65fab584715e786ce3264a6932d996ec/Python/quickstarts/pubsub-helloworld/functions/main.py#L29-L30\n\nAccess message attributes\n\nPub/Sub message can be sent with data attributes set in the\npublish command. For example, you could publish a message with a `name`\nattribute: \n\n gcloud pubsub topics publish topic-name --attribute name=Xenia\n\nYou can read such attributes from the corresponding property of the\nmessage object: \n\nNode.js \n\n```javascript\n// Get the `name` attribute of the message.\nconst name = event.data.message.attributes.name;https://github.com/firebase/functions-samples/blob/c4fde45b65fab584715e786ce3264a6932d996ec/Node/quickstarts/pubsub-helloworld/functions/index.js#L71-L72\n```\n\nPython \n\n # Get the `name` attribute of the message.\n if \"name\" not in event.data.message.attributes:\n print(\"No 'name' attribute\")\n return\n name = event.data.message.attributes[\"name\"] \n https://github.com/firebase/functions-samples/blob/c4fde45b65fab584715e786ce3264a6932d996ec/Python/quickstarts/pubsub-helloworld/functions/main.py#L64-L68"]]