Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Vous pouvez utiliser un connecteur Workflows pour prendre en charge les opérations Pub/Sub, y compris la publication de messages dans un sujet Pub/Sub.
Un sujet Pub/Sub est une ressource à laquelle les éditeurs envoient des messages. Un abonnement représente le flux de messages d'un sujet à distribuer dans l'application d'abonnement.
En savoir plus sur Pub/Sub
Publier des messages
Une fois un sujet Pub/Sub et un abonnement à ce sujet créés, vous pouvez créer un workflow qui publie un message sur ce sujet:
TOPIC_ID: ID ou identifiant complet du sujet Pub/Sub.
SUBSCRIPTION_ID: ID ou identifiant complet de l'abonnement Pub/Sub.
Extraire des messages
Vous pouvez créer un déclencheur Eventarc qui connecte un sujet Pub/Sub à un récepteur d'événements Workflows. Un message est publié dans un sujet Pub/Sub pour générer un événement, qui est transmis en tant qu'argument d'exécution au workflow de destination. Pour en savoir plus, consultez la page Déclencher un workflow avec des événements ou des messages Pub/Sub.
Vous pouvez également créer un workflow qui extrait le message Pub/Sub.
Dans l'exemple suivant, le workflow attend la publication du message à l'aide d'une interrogation.
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/09/10 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 2025/09/10 (UTC)."],[],[],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```"]]