Setup Google Cloud PubSub (Python CLI)

July 17, 2019

First, Setup Google Cloud PubSub.

Install python libraries.

pip install --upgrade google-cloud-pubsub

Create Service Account

Access Google Cloud Console -> Create service account key.

  • Service account: New service account
  • Service account name: pubsub
  • Role: Datastore -> Pub/Sub Admin or Editor
  • Key type: JSON

Click create. Save the JSON file PROJECT_ID-pubsub.json.

Publish messages

from import pubsub_v1

PROJECT_ID = '...'
TOPIC_NAME = '...'

# publisher = pubsub_v1.PublisherClient()
publisher = pubsub_v1.PublisherClient.from_service_account_file(SERVICE_ACCOUNT_FILE)
topic_path = publisher.topic_path(PROJECT_ID, TOPIC_NAME)

# data must be bytes
# attributes must be string only
future = publisher.publish(topic_path, data='Hello'.encode('utf-8'), name='Desmond', age='40')

Subscribe messages

import time
from import pubsub_v1

PROJECT_ID = '...'

# subscriber = pubsub_v1.SubscriberClient()
subscriber = pubsub_v1.SubscriberClient.from_service_account_file(SERVICE_ACCOUNT_FILE)
subscription_path = subscriber.subscription_path(PROJECT_ID, SUBSRIPTION_NAME)

def callback(message):
    print(' {}'.format(
    if message.attributes:
        for key in message.attributes:
            value = message.attributes.get(key)
            print(f"{key}: {value}")

future = subscriber.subscribe(subscription_path, callback=callback)

# The subscriber is non-blocking. We must keep the main thread from
# exiting to allow it to process messages asynchronously in the background.
print(f"Listening for messages: {subscription_path}")
while True:
    print('.', end='', flush=True)


This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.