Google Cloud: Working with Pub/Sub with Command Line

Share At:

Overview

Google Cloud Pub/Sub is a messaging service for exchanging event data among applications and services. By decoupling senders and receivers, it allows for secure and highly available communication between independently written applications. Google Cloud Pub/Sub delivers low-latency/durable messaging, and is commonly used by developers in implementing asynchronous workflows, distributing event notifications, and streaming data from various processes or devices.

Lab Task

In this lab, you will do the following:

  • Learn the basics of Pub/Sub.
  • Create, delete, and list Pub/Sub topics.
  • Create, delete, and list Pub/Sub subscriptions.
  • Publish messages to a topic.
  • Use a pull subscriber to output individual topic messages.
  • Use a pull subscriber with a flag to output multiple messages.

The Pub/Sub basics

As stated earlier, Google Cloud Pub/Sub is an asynchronous global messaging service. There are three terms in Pub/Sub that appear often: topics, publishing, and subscribing.

  • topic is a shared string that allows applications to connect with one another through a common thread.
  • Publishers push (or publish) a message to a Cloud Pub/Sub topic.
  • Subscribers make a “subscription” to a topic where they will either pull messages from the subscription or configure webhooks for push subscriptions. Every subscriber must acknowledge each message within a configurable window of time.

To sum it up, a producer publishes messages to a topic and a consumer creates a subscription to a topic to receive messages from it.

Pub/Sub topics

Pub/Sub comes preinstalled in the Google Cloud Shell, so there are no installations or configurations required to get started with this service.

  1. Run the following command to create a topic called myTopic:
gcloud pubsub topics create myTopic

2. For good measure, create two more topics; one called Test1 and the other called Test2:

gcloud pubsub topics create Test1gcloud pubsub topics create Test2

3. To see the three topics you just created, run the following command:

gcloud pubsub topics list

Your output should resemble the following:

4. Time to cleanup. Delete Test1 and Test2 by running the following commands:

gcloud pubsub topics delete Test1gcloud pubsub topics delete Test2

5. Run the gcloud pubsub topics list command one more time to verify the topics were deleted:

gcloud pubsub topics list

You should get the following output:

Pub/Sub subscriptions

Now that you’re comfortable creating, viewing, and deleting topics, time to work with subscriptions.

  1. Run the following command to create a subscription called mySubscription to topic myTopic:
gcloud  pubsub subscriptions create --topic myTopic mySubscription

2. Add another two subscriptions to myTopic.

Run the following commands to make Test1 and Test2 subscriptions:

gcloud  pubsub subscriptions create --topic myTopic Test1gcloud  pubsub subscriptions create --topic myTopic Test2

3. Run the following command to list the subscriptions to myTopic:

gcloud pubsub topics list-subscriptions myTopic

Your output should resemble the following:

4. Now delete the Test1 and Test2 subscriptions. Run the following commands:

gcloud pubsub subscriptions delete Test1gcloud pubsub subscriptions delete Test2

5. See if the Test1 and Test2 subscriptions were deleted. Run the list-subscriptions command one more time:

gcloud pubsub topics list-subscriptions myTopic

You should get the following output:

Pub/Sub Publishing and Pulling a Single Message

Next we’ll learn how to publish a message to a Pub/Sub topic.

  1. Run the following command to publish the message "hello" to the topic you created previously (myTopic):
gcloud pubsub topics publish myTopic --message "Hello"

2. Publish a few more messages to myTopic.

Run the following commands (replacing <YOUR NAME> with your name and <FOOD> with a food you like to eat):

gcloud pubsub topics publish myTopic --message "Publisher's name is <YOUR NAME>"gcloud pubsub topics publish myTopic --message "Publisher likes to eat <FOOD>"gcloud pubsub topics publish myTopic --message "Publisher thinks Pub/Sub is awesome"

3. Next, use the pull command to get the messages from your topic.

The pull command is subscription based, meaning it should work because earlier you set up the subscription mySubscription to the topic myTopic.

Use the following command to pull the messages you just published from the Pub/Sub topic:

gcloud pubsub subscriptions pull mySubscription --auto-ack

Your output should resemble the following:

What’s going on here?

You published 4 messages to your topic, but only 1 was outputted.

Now is an important time to note a couple features of the pull command that often trip developers up:

  • Using the pull command without any flags will output only one message, even if you are subscribed to a topic that has more held in it.
  • Once an individual message has been outputted from a particular subscription-based pull command, you cannot access that message again with the pull command.

4. To see what the second bullet is talking about, run the last command three more times. You will see that it will output the other messages you published before.

5. Now, run the command a 4th time. You’ll get the following output (since there were none left to return):

In the last section, we will learn how to pull multiple messages from a topic with a flag.

Pub/Sub pulling all messages from subscriptions

  1. Since you pulled all of the messages from your topic in the last example, populate myTopic with a few more messages.

Run the following commands:

gcloud pubsub topics publish myTopic --message "Publisher is starting to get the hang of Pub/Sub"gcloud pubsub topics publish myTopic --message "Publisher wonders if all messages will be pulled"gcloud pubsub topics publish myTopic --message "Publisher will have to test to find out"

2. Add a flag to your command so you can output all three messages in one request. limitis another flag that sets an upper limit on the number of messages to pull.

Wait a minute to let the topics get created. Run the pull command with the limit flag:

gcloud pubsub subscriptions pull mySubscription --auto-ack --limit=3

Your output should match the following:

Now you know how to add flags to a Pub/Sub command to output a larger pool of messages.

Happy Learning !!!


Share At:
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Back To Top

Contact Us