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:
- https://cloud.google.com/pubsub/docs/quickstart-client-libraries
- https://googleapis.github.io/google-cloud-python/latest/pubsub/
- https://google-cloud.readthedocs.io/en/latest/pubsub/index.html
- https://google-cloud.readthedocs.io/en/latest/pubsub/publisher/api/client.html
- https://googleapis.github.io/google-cloud-python/latest/pubsub/subscriber/api/client.html
- https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/pubsub/cloud-client