Comprehensive Guide to Cloud Pub/Sub
Introduction
Cloud Pub/Sub is a messaging service provided by Google Cloud that enables you to send and receive messages between independent applications. It is designed to provide reliable, many-to-many, asynchronous messaging between decoupled systems.
Setting Up Cloud Pub/Sub
To get started with Cloud Pub/Sub, you need a Google Cloud project. Follow these steps:
Step 1: Create a new project in the Google Cloud Console.
Step 2: Enable the Cloud Pub/Sub API for your project.
Step 3: Set up authentication by creating a service account and downloading the JSON key file.
Creating a Topic
A topic is a named resource to which messages are sent by publishers. To create a topic, use the following command:
gcloud pubsub topics create my-topic
Created topic [projects/YOUR_PROJECT_ID/topics/my-topic].
Publishing Messages
Publish messages to the topic using the following command:
gcloud pubsub topics publish my-topic --message "Hello, World!"
messageIds: 123456789012345
Creating a Subscription
A subscription is a named resource representing the stream of messages from a single, specific topic, to be delivered to the subscribing application. To create a subscription, use the following command:
gcloud pubsub subscriptions create my-subscription --topic my-topic
Created subscription [projects/YOUR_PROJECT_ID/subscriptions/my-subscription].
Receiving Messages
Receive messages from the subscription using the following command:
gcloud pubsub subscriptions pull my-subscription --auto-ack
┌──────────────────────┬───────────────┐
│ DATA │ MESSAGE_ID │
├──────────────────────┼───────────────┤
│ Hello, World! │ 1234567890123 │
└──────────────────────┴───────────────┘
Example Application
Here's a simple example of a Python application that publishes and receives messages using Cloud Pub/Sub:
Publisher (publish.py):
import os from google.cloud import pubsub_v1 os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "path/to/your/service-account-file.json" publisher = pubsub_v1.PublisherClient() topic_path = publisher.topic_path('YOUR_PROJECT_ID', 'my-topic') data = 'Hello, World!' data = data.encode('utf-8') future = publisher.publish(topic_path, data) print(f'Published message ID: {future.result()}')
Subscriber (subscribe.py):
import os from google.cloud import pubsub_v1 os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "path/to/your/service-account-file.json" subscriber = pubsub_v1.SubscriberClient() subscription_path = subscriber.subscription_path('YOUR_PROJECT_ID', 'my-subscription') def callback(message): print(f'Received message: {message.data}') message.ack() subscriber.subscribe(subscription_path, callback=callback) print('Listening for messages on {}'.format(subscription_path)) while True: pass
Conclusion
Cloud Pub/Sub is a powerful tool for building scalable and reliable messaging systems. By following this tutorial, you should now have a good understanding of how to create topics and subscriptions, publish and receive messages, and implement a simple application using Cloud Pub/Sub.