Utilizing Google Cloud Pub/Sub

30 minutes
  • 7 Learning Objectives

About this Hands-on Lab

Cloud Pub/Sub is Google Cloud’s central fully managed messaging service. Being central, it is consequently incorporated in a lot of different workflows. Pub/Sub can be used to stream data to BigQuery, Cloud Functions, Stackdriver, Cloud IoT Core, and numerous other services. This hands-on lab will provide you with practical experience working with the necessary components of Cloud Pub/Sub: topics, subscriptions, and messages.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Enable APIs
  1. From the main console navigation, go to APIs and Services > Library.
  2. Search for Pub/Sub.
  3. Select the Cloud Pub/Sub card.
  4. Click Enable, if displayed.
Create a Topic
  1. From the main navigation, go to Pub/Sub > Topics.
  2. Click Create a topic.
  3. Enter a name for the topic (e.g., "acg-topic-1").
  4. Deselect Create a default subscription.
  5. Click Create.
Create a Subscription
  1. Drill down into the topic just created and choose Subscriptions.
  2. Click the icon in the Topic details navigation bar to create a subscription.
  3. Enter a name for the subscription (e.g., "acg-sub-1").
  4. Set Delivery Type to Pull.
  5. Leave the rest of the settings at their defaults.
  6. Click Create.
Publish a Message
  1. From the Topic details page, click Publish Message.
  2. Enter a message in the Message field.
  3. Click Publish.
Retrieve Message
  1. Activate the Cloud Shell by clicking its icon in the top row.

  2. If the Cloud Shell does not display the current project ID in yellow, open the Project panel by clicking the project name in the upper-left.

  3. Copy the current project ID.

  4. In the Cloud Shell, enter the following command:

    gcloud config set project <PROJECT_ID>
  5. To retrieve a message, enter the following command:

    gcloud pubsub subscriptions pull <SUBSCRIPTION_NAME> --auto-ack
  6. Confirm the message displayed is the same as published.

  7. Repeat the command again.

  8. Confirm zero messages are found.

Publish Multiple Messages
  1. From the Topic details page, click Publish Message.
  2. Enter a message in the Message field.
  3. Click Publish.
  4. Repeat steps 1-3 twice more.
Retrieve Multiple Messages
  1. In the Cloud Shell, enter the following command:

    gcloud pubsub subscriptions pull <SUBSCRIPTION_NAME> --auto-ack --limit=3
  2. Confirm all messages displayed are the same as were published.

Additional Resources

You’ve been asked to head up the new Cloud Pub/Sub team, responsible for incorporating the messaging service into various apps deployed across the Google Cloud platform. In order to understand how the system works, you’ll need to create a Cloud Pub/Sub topic, subscriber, and multiple messages. You’ll also need to retrieve the messages from the command line using the pertinent gcloud commands.

To accomplish this task, you’ll need to complete the following steps:

  1. Enable the appropriate APIs.
  2. Create a Cloud Pub/Sub topic.
  3. Create a Cloud Pub/Sub subscription.
  4. Publish a message.
  5. Retrieve the message.
  6. Publish multiple messages.
  7. Retrieve all messages.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?