Pub/Sub 메시지의 페이로드는 함수에 반환된 Message 객체에서 액세스할 수 있습니다. Pub/Sub 메시지 본문에 JSON이 포함된 경우 Cloud Functions용 Firebase의 도우미 속성으로 메시지를 디코딩할 수 있습니다. 예를 들어 다음은 간단한 JSON 페이로드와 함께 게시된 메시지입니다.
//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-09(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. You\ncan create a function that handles Pub/Sub events by using\n[`functions.pubsub`](/docs/reference/functions/firebase-functions.pubsub).\n\nTrigger a pub/sub function\n\nYou can trigger a function whenever a new Pub/Sub message is sent\nto a specific topic. You must specify the Pub/Sub topic name that\nyou want to trigger your function, and set the event within the\n[`onPublish()`](/docs/reference/functions/firebase-functions.pubsub.topicbuilder#pubsubtopicbuilderonpublish)\nevent handler:\n\n\u003cbr /\u003e\n\n```gdscript\nexports.helloPubSub = functions.pubsub.topic('topic-name').onPublish((message) =\u003e {\n // ...\n});\n```\n\n\u003cbr /\u003e\n\nAccess the pub/sub message payload\n\nThe payload for the Pub/Sub message is accessible from the\n[`Message`](/docs/reference/functions/firebase-functions.pubsub.message) 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`](/docs/reference/functions/firebase-functions.pubsub.message#pubsubmessagejson) property: \n\n```mysql\n // Get the `name` attribute of the PubSub message JSON body.\n let name = null;\n try {\n name = message.json.name;\n } catch (e) {\n functions.logger.error('PubSub message was not JSON', e);\n }https://github.com/firebase/functions-samples/blob/c4fde45b65fab584715e786ce3264a6932d996ec/Node-1st-gen/quickstarts/pubsub-helloworld/functions/index.js#L46-L52\n```\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\n\u003cbr /\u003e\n\n```gdscript\n// Decode the PubSub Message body.\nconst messageBody = message.data ? Buffer.from(message.data, 'base64').toString() : null;https://github.com/firebase/functions-samples/blob/c4fde45b65fab584715e786ce3264a6932d996ec/Node-1st-gen/quickstarts/pubsub-helloworld/functions/index.js#L31-L32\n```\n\n\u003cbr /\u003e\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\n[`Message.attributes`](/docs/reference/functions/firebase-functions.pubsub#pubsubmessageattributes):\n\n\u003cbr /\u003e\n\n```mysql\n// Get the `name` attribute of the message.\nconst name = message.attributes.name;https://github.com/firebase/functions-samples/blob/c4fde45b65fab584715e786ce3264a6932d996ec/Node-1st-gen/quickstarts/pubsub-helloworld/functions/index.js#L65-L66\n```\n\n\u003cbr /\u003e\n\nYou might notice that some basic data such as the message ID or the\nmessage publish time are not available in `Message.attributes`. To work around\nthis, you can access these details in the triggering event's\n[`EventContext`](/docs/reference/functions/firebase-functions.eventcontext).\nFor example: \n\n exports.myFunction = functions.pubsub.topic('topic1').onPublish((message, context) =\u003e {\n console.log('The function was triggered at ', context.timestamp);\n console.log('The unique ID for the event is', context.eventId);\n });"]]