The Pub/Sub service allows applications to exchange messages reliably,
quickly, and asynchronously.
To accomplish this, a producer of data publishes a messages to a Pub/Sub topic.
A subscriber client then creates a subscription to that topic and consumes
messages from the subscription. Pub/Sub persists messages that could not be
delivered reliably for up to seven days.
This page shows you how to get started publishing messages with Pub/Sub
using client libraries. If you don't already have one, sign up for a new account. Click to: You can view and manage these resources at any time in the Cloud Console. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the path of the JSON file that contains your service account key. This variable only applies to your current shell session, so if you open a new session, set the variable again. If you are using the SDK (rather than the Cloud Shell), install the client
libraries in your programming language of choice: Once you create a topic, you can subscribe or publish to it. Use the
gcloud pubsub topics create command to create a topic: Use the
gcloud pubsub subscriptions create command to create a subscription. Only messages published
to the topic after the subscription is created are available to subscriber
applications. For more information about naming your topics and subscriptions, see
Resource names. Set up a subscriber to pull the messages you just published. Every subscriber
must acknowledge each message within a configurable time window. Unacknowledged
messages are redelivered. Note that Pub/Sub occasionally
delivers a message more than once to ensure that all messages make it to a
subscriber at least once. Here is an example of how you might receive and
acknowledge messages: To avoid incurring charges to your Google Cloud Platform account for the
resources used in this guide, you can use the command line to delete the topic and subscription.
gcloud pubsub topics create my-topic
gcloud pubsub subscriptions create my-sub --topic my-topic
Publishing messages
Receiving messages
How did it go?
Clean up (optional)
gcloud pubsub subscriptions delete my-sub gcloud pubsub topics delete my-topic