Pub/Sub#

Google Cloud Pub/Sub is a fully-managed real-time messaging service that allows you to send and receive messages between independent applications. You can leverage Cloud Pub/Sub’s flexibility to decouple systems and components hosted on Google Cloud Platform or elsewhere on the Internet. By building on the same technology Google uses, Cloud Pub/Sub is designed to provide “at least once” delivery at low latency with on-demand scalability to 1 million messages per second (and beyond).

Authentication and Configuration#

  • For an overview of authentication in google-cloud-python, see Authentication.

  • In addition to any authentication configuration, you should also set the GOOGLE_CLOUD_PROJECT environment variable for the project you’d like to interact with. If the GOOGLE_CLOUD_PROJECT environment variable is not present, the project ID from JSON file credentials is used.

    If you are using Google App Engine or Google Compute Engine this will be detected automatically.

  • After configuring your environment, create a PublisherClient or SubscriberClient.

>>> from google.cloud import pubsub
>>> publisher = pubsub.PublisherClient()
>>> subscriber = pubsub.SubscriberClient()

or pass in credentials explicitly.

>>> from google.cloud import pubsub
>>> client = pubsub.PublisherClient(
...     credentials=creds,
... )

Publishing#

To publish data to Cloud Pub/Sub you must create a topic, and then publish messages to it

>>> import os
>>> from google.cloud import pubsub
>>>
>>> publisher = pubsub.PublisherClient()
>>> topic = 'projects/{project_id}/topics/{topic}'.format(
...     project_id=os.getenv('GOOGLE_CLOUD_PROJECT'),
...     topic='MY_TOPIC_NAME',  # Set this to something appropriate.
... )
>>> publisher.create_topic()
>>> publisher.publish(topic, b'My first message!', spam='eggs')

To learn more, consult the publishing documentation.

Subscribing#

To subscribe to data in Cloud Pub/Sub, you create a subscription based on the topic, and subscribe to that.

>>> import os
>>> from google.cloud import pubsub
>>>
>>> subscriber = pubsub.SubscriberClient()
>>> topic = 'projects/{project_id}/topics/{topic}'.format(
...     project_id=os.getenv('GOOGLE_CLOUD_PROJECT'),
...     topic='MY_TOPIC_NAME',  # Set this to something appropriate.
... )
>>> subscription_name = 'projects/{project_id}/subscriptions/{sub}'.format(
...     project_id=os.getenv('GOOGLE_CLOUD_PROJECT'),
...     sub='MY_SUBSCRIPTION_NAME',  # Set this to something appropriate.
... )
>>> subscription = subscriber.create_subscription(subscription_name, topic)

The subscription is opened asychronously, and messages are processed by use of a callback.

>>> def callback(message):
...     print(message.data)
...     message.ack()
>>> subscription.open(callback)

To learn more, consult the subscriber documentation.