[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-04 (世界標準時間)。"],[],[],null,["# Publish a message to a Pub/Sub topic\n\nYou can use a\n[Workflows connector](/workflows/docs/reference/googleapis/pubsub/Overview)\nto support Pub/Sub operations, including publishing messages to\na Pub/Sub topic.\n\nA Pub/Sub topic is a resource to which messages are sent by\npublishers. A subscription represents the stream of messages from a topic that\nare to be delivered to the subscribing application.\n[Learn more about Pub/Sub](/pubsub/docs/overview).\n\nPublishing messages\n-------------------\n\nOnce a Pub/Sub topic and a subscription to that topic has been\ncreated, you can\n[create a workflow](/workflows/docs/creating-updating-workflow#create_a_workflow)\nthat publishes a message to that topic: \n\n### YAML\n\n```yaml\n- init:\n assign:\n - project: '${sys.get_env(\"GOOGLE_CLOUD_PROJECT_ID\")}'\n - topic: TOPIC_ID\n - subscription: SUBSCRIPTION_ID\n - message:\n hello: world\n - base64Msg: '${base64.encode(json.encode(message))}'\n- publish_message_to_topic:\n call: googleapis.pubsub.v1.projects.topics.publish\n args:\n topic: '${\"projects/\" + project + \"/topics/\" + topic}'\n body:\n messages:\n - data: '${base64Msg}'\n```\n\n### JSON\n\n```json\n[\n{\n \"init\": {\n \"assign\": [\n {\n \"project\": \"${sys.get_env(\\\"GOOGLE_CLOUD_PROJECT_ID\\\")}\"\n },\n {\n \"topic\": \"\u003cvar translate=\"no\"\u003eTOPIC_ID\u003c/var\u003e\"\n },\n {\n \"subscription\": \"\u003cvar translate=\"no\"\u003eSUBSCRIPTION_ID\u003c/var\u003e\"\n },\n {\n \"message\": {\n \"hello\": \"world\"\n }\n },\n {\n \"base64Msg\": \"${base64.encode(json.encode(message))}\"\n }\n ]\n }\n},\n{\n \"publish_message_to_topic\": {\n \"call\": \"googleapis.pubsub.v1.projects.topics.publish\",\n \"args\": {\n \"topic\": \"${\\\"projects/\\\" + project + \\\"/topics/\\\" + topic}\",\n \"body\": {\n \"messages\": [\n {\n \"data\": \"${base64Msg}\"\n }\n ]\n }\n }\n }\n}\n]\n```\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003eTOPIC_ID\u003c/var\u003e: the ID or fully qualified identifier for\n the Pub/Sub topic.\n\n- \u003cvar translate=\"no\"\u003eSUBSCRIPTION_ID\u003c/var\u003e: the ID or fully qualified identifier\n for the Pub/Sub subscription.\n\nPulling messages\n----------------\n\nYou can create an Eventarc trigger that connects a Pub/Sub\ntopic to a Workflows event receiver. A message is published to a\nPub/Sub topic to generate an event, and the event is passed as a\nruntime argument to the destination workflow. For more information, see\n[Trigger a workflow with events or Pub/Sub messages](/workflows/docs/trigger-workflow-eventarc).\n\nYou can also create a workflow that pulls the Pub/Sub message.\nIn the following example, the workflow waits for the message to be published\nusing [polling](/workflows/docs/sleeping). \n\n### YAML\n\n```yaml\n- pullMessage:\n call: googleapis.pubsub.v1.projects.subscriptions.pull\n args:\n subscription: '${\"projects/\" + project + \"/subscriptions/\" + subscription}'\n body:\n maxMessages: 1\n result: m\n- checkState:\n switch:\n - condition: ${m.receivedMessages[0].message.data != \"\"}\n next: outputMessage\n- wait:\n call: sys.sleep\n args:\n seconds: 60\n next: pullMessage\n- outputMessage:\n return: '${json.decode(base64.decode(m.receivedMessages[0].message.data))}'\n```\n\n### JSON\n\n```json\n [\n {\n \"pullMessage\": {\n \"call\": \"googleapis.pubsub.v1.projects.subscriptions.pull\",\n \"args\": {\n \"subscription\": \"${\\\"projects/\\\" + project + \\\"/subscriptions/\\\" + subscription}\",\n \"body\": {\n \"maxMessages\": 1\n }\n },\n \"result\": \"m\"\n }\n },\n {\n \"checkState\": {\n \"switch\": [\n {\n \"condition\": \"${m.receivedMessages[0].message.data != \\\"\\\"}\",\n \"next\": \"outputMessage\"\n }\n ]\n }\n },\n {\n \"wait\": {\n \"call\": \"sys.sleep\",\n \"args\": {\n \"seconds\": 60\n },\n \"next\": \"pullMessage\"\n }\n },\n {\n \"outputMessage\": {\n \"return\": \"${json.decode(base64.decode(m.receivedMessages[0].message.data))}\"\n }\n }\n ]\n```"]]