تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
Pub/Sub من Google Cloud هو ناقل رسائل موزّع على مستوى العالم يتم توسيعه تلقائيًا حسب حاجتك. يمكنك إنشاء دالة تعالج أحداث Pub/Sub باستخدام functions.pubsub.
تشغيل دالة النشر/الاشتراك
يمكنك تشغيل دالة كلما تم إرسال رسالة Pub/Sub جديدة
إلى موضوع معيّن. يجب تحديد اسم موضوع Pub/Sub الذي تريد أن يؤدي إلى تشغيل الدالة، وضبط الحدث ضمن معالج الأحداث onPublish():
الوصول إلى حمولة رسالة Pub/Sub {:#access-pub/sub}
يمكن الوصول إلى حمولة الرسالة Pub/Sub من العنصر Message الذي تم إرجاعه إلى الدالة. بالنسبة إلى الرسائل التي تتضمّن JSON في نص الرسالة Pub/Sub، تحتوي حزمة تطوير البرامج (SDK) Firebase الخاصة بـ Cloud Functions على خاصية مساعِدة لفك ترميز الرسالة. على سبيل المثال، إليك رسالة تم نشرها باستخدام حمولة JSON بسيطة:
يمكنك الوصول إلى حِمل بيانات JSON على النحو التالي من خلال السمة
json:
//Getthe`name`attributeofthePubSubmessageJSONbody.letname=null;try{name=message.json.name;}catch(e){functions.logger.error('PubSub message was not JSON',e);}
يتم تضمين الحمولات الأخرى غير JSON في الرسالة Pub/Sub كسلاسل بترميز base64 في عنصر الرسالة. لقراءة رسالة مثل الرسالة التالية، عليك فك ترميز السلسلة المرمّزة باستخدام base64 كما هو موضّح:
قد تلاحظ أنّ بعض البيانات الأساسية، مثل معرّف الرسالة أو وقت نشر الرسالة، غير متوفّرة في Message.attributes. لحلّ هذه المشكلة، يمكنك الوصول إلى هذه التفاصيل في EventContext الحدث المشغِّل.
على سبيل المثال:
exports.myFunction=functions.pubsub.topic('topic1').onPublish((message,context)=>{console.log('The function was triggered at ',context.timestamp);console.log('The unique ID for the event is',context.eventId);});
تاريخ التعديل الأخير: 2025-09-05 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","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-05 (حسب التوقيت العالمي المتفَّق عليه)"],[],[],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"]]