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
- From the main console navigation, go to APIs and Services > Library.
- Search for Pub/Sub.
- Select the Cloud Pub/Sub card.
- Click Enable, if displayed.
- Create a Topic
- From the main navigation, go to Pub/Sub > Topics.
- Click Create a topic.
- Enter a name for the topic (e.g., "acg-topic-1").
- Deselect Create a default subscription.
- Click Create.
- Create a Subscription
- Drill down into the topic just created and choose Subscriptions.
- Click the icon in the Topic details navigation bar to create a subscription.
- Enter a name for the subscription (e.g., "acg-sub-1").
- Set Delivery Type to Pull.
- Leave the rest of the settings at their defaults.
- Click Create.
- Publish a Message
- From the Topic details page, click Publish Message.
- Enter a message in the Message field.
- Click Publish.
- Retrieve Message
Activate the Cloud Shell by clicking its icon in the top row.
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.
Copy the current project ID.
In the Cloud Shell, enter the following command:
gcloud config set project <PROJECT_ID>
To retrieve a message, enter the following command:
gcloud pubsub subscriptions pull <SUBSCRIPTION_NAME> --auto-ack
Confirm the message displayed is the same as published.
Repeat the command again.
Confirm zero messages are found.
- Publish Multiple Messages
- From the Topic details page, click Publish Message.
- Enter a message in the Message field.
- Click Publish.
- Repeat steps 1-3 twice more.
- Retrieve Multiple Messages
In the Cloud Shell, enter the following command:
gcloud pubsub subscriptions pull <SUBSCRIPTION_NAME> --auto-ack --limit=3
Confirm all messages displayed are the same as were published.