Introduction: Messaging with Pub/Sub

Welcome back! So far, you have learned how to work with Google Cloud services like Compute Engine, Cloud Firestore, and Cloud Datastore. In this lesson, we will explore another important Google Cloud service: Pub/Sub (Publish/Subscribe).

In modern cloud applications, different parts of your system often need to communicate with each other. Sometimes, you want to send a message from one part of your application to another, but you do not want them to be tightly connected. This is where messaging services like Pub/Sub come in. They help you build applications that are more flexible, reliable, and easier to scale.

By the end of this lesson, you will know how to:

  • Create a Pub/Sub topic and a subscription.
  • Publish messages to a Pub/Sub topic (acting as a producer).
  • Receive and process messages from a subscription (acting as a consumer).
  • Publish results to another Pub/Sub topic.

Let’s get started!

Creating Pub/Sub Topics and Subscriptions

The first step is to create the messaging resources: a Pub/Sub topic and a subscription. The topic is where messages will be published, and the subscription is how messages are received.

Let’s start by importing the necessary libraries and creating unique names for our resources:

  • publisher.create_topic(...) creates a new Pub/Sub topic.
  • subscriber.create_subscription(...) creates a new subscription to the topic.
  • We use uuid to generate unique names for the topic and subscription.

Example Output:

Now you have a Pub/Sub topic and a subscription ready to use!

Sending Messages to Pub/Sub

Next, let’s act as a producer and publish messages to the Pub/Sub topic. In this example, we will send two simple order messages.

First, let’s prepare the messages and publish them to the topic:

  • We create a list of orders, each represented as a dictionary.
  • We use json.dumps(o).encode("utf-8") to convert each order to a JSON string and encode it as bytes, which is the format Pub/Sub expects.
  • publisher.publish(topic_path, data) publishes the message to the topic.

Example Output:

This is how you publish messages to a Pub/Sub topic. The producer part is now complete.

Consuming and Processing Messages from Pub/Sub

Now, let’s act as a consumer. We will pull messages from the Pub/Sub subscription, process them, and then acknowledge them so they are not delivered again.

Here’s how you can receive and process messages:

  • subscriber.pull(...) fetches up to 5 messages from the subscription.
  • We use json.loads(...decode("utf-8")) to convert the message back to a dictionary.
  • The process_order function simulates processing the order.
  • After processing, we call subscriber.acknowledge(...) to acknowledge the messages so they are not delivered again.

Example Output:

This completes the consumer part. You have now received and processed messages from Pub/Sub.

Publishing Results to Another Pub/Sub Topic

After processing each order, let’s publish the result to another Pub/Sub topic. This is useful if you want to notify other parts of your system or trigger further actions.

First, create a new topic for processed results:

Now, after processing each order, publish the result to the new topic:

  • publisher.publish(result_topic_path, ...) sends the processed order to the result topic.
  • This allows other systems or users subscribed to the topic to receive notifications.

Example Output:

Now, every processed order is published to another Pub/Sub topic, making your application more flexible and event-driven.

Summary and Next Steps

In this lesson, you learned how to:

  • Create a Pub/Sub topic and a subscription.
  • Publish messages to a Pub/Sub topic as a producer.
  • Receive, process, and acknowledge messages from a subscription as a consumer.
  • Publish processed results to another Pub/Sub topic.

This producer/consumer pattern is a powerful way to build decoupled and scalable applications in Google Cloud. You are now ready to practice these skills with hands-on exercises. Try creating your own topics and subscriptions, sending and processing messages, and publishing results to other topics. This will help you become more comfortable with event-driven development in Google Cloud. Good luck!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal