//Getthe`name`attributeofthePubSubmessageJSONbody.letname=null;try{name=message.json.name;}catch(e){functions.logger.error('PubSub message was not JSON',e);}
Message.attributes では、メッセージ ID やメッセージをパブリッシュする時間など、一部の基本的なデータが使用できないことがあります。この問題を回避するには、トリガー イベントの 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);});
[[["わかりやすい","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 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"]]