Setup Google Cloud PubSub (Python CLI)

Jul 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 google.cloud import pubsub_v1PROJECT_ID = '...'TOPIC_NAME = '...'SERVICE_ACCOUNT_FILE = 'PROJECT_ID-pubsub.json'# 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 onlyfuture = publisher.publish(topic_path, data='Hello'.encode('utf-8'), name='Desmond', age='40')print(future.result())

Subscribe messages

import timefrom google.cloud import pubsub_v1PROJECT_ID = '...'SUBSRIPTION_NAME = '...'SERVICE_ACCOUNT_FILE = 'PROJECT_ID-pubsub.json'# 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('message.data: {}'.format(message.data))    if message.attributes:        for key in message.attributes:            value = message.attributes.get(key)            print(f"{key}: {value}")    message.ack()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)    time.sleep(5)

References:

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.