Connector for Pub/Sub

Workflows connector that defines the built-in function used to access Pub/Sub within a workflow.

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample

YAML

# This workflow demonstrates how to use the Cloud Pub/Sub connector.
# The workflow creates a Pub/Sub topic, a new subscription to that new topic, publishes a new message to
# the topic, pulls the message from the subscription, and then deletes both
# the subscription and the topic.
# Expected successful output: "SUCCESS"

- init:
    assign:
      - project: ${sys.get_env("GOOGLE_CLOUD_PROJECT_ID")}
      - topic: "[fill in a Pubsub topic name]"
      - subscription: "[fill in a Pubsub subscription name]"
      - message: {"hello": "world"}  # An object used as a secret.
      - base64Msg: ${base64.encode(json.encode(message))}
- create_topic:
    call: googleapis.pubsub.v1.projects.topics.create
    args:
      name: ${"projects/" + project + "/topics/" + topic}
- create_subscription_to_topic:
    call: googleapis.pubsub.v1.projects.subscriptions.create
    args:
      name: ${"projects/" + project + "/subscriptions/" + subscription}
      body:
        name: ${"projects/" + project + "/subscriptions/" + subscription}
        topic: ${"projects/" + project + "/topics/" + topic}
- publish_message_to_topic:
    call: googleapis.pubsub.v1.projects.topics.publish
    args:
      topic: ${"projects/" + project + "/topics/" + topic}
      body:
        messages:
          - data: ${base64Msg}
- pull_message:
    call: googleapis.pubsub.v1.projects.subscriptions.pull
    args:
      subscription: ${"projects/" + project + "/subscriptions/" + subscription}
      body:
        maxMessages: 1
    result: m
- check_message:
    switch:
      - condition: ${m.receivedMessages[0].message.data != base64Msg}
        next: failed
- delete_subscription:
    call: googleapis.pubsub.v1.projects.subscriptions.delete
    args:
      subscription: ${"projects/" + project + "/subscriptions/" + subscription}
- delete_topic:
    call: googleapis.pubsub.v1.projects.topics.delete
    args:
      topic: ${"projects/" + project + "/topics/" + topic}
- the_end:
    return: "SUCCESS"
- failed:
    raise: ${"got data:" + m.receivedMessages[0].message.data + " want data:" + base64Msg}

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.