Overview
Pub/Sub is a messaging service for exchanging event data among applications and services. A producer of data publishes messages to a Pub/Sub topic. A consumer creates a subscription to that topic. Subscribers either pull messages from a subscription or are configured as webhooks for push subscriptions. Every subscriber must acknowledge each message within a configurable window of time.
What you'll learn
Set up a topic to hold data.
Subscribe to a topic to access the data.
Publish and then consume messages with a pull subscriber.
Task 1. Setting up Pub/Sub
To use Pub/Sub, you create a topic to hold data and a subscription to access data published to the topic.
From the Navigation menu (
) go to Pub/Sub > Topics.
- Click Create topic.
- The topic must have a unique name. For this lab, name your topic
MyTopic
. In the Create a topic dialog:
For Topic ID, type
MyTopic
.Leave other fields at their default value.
Click CREATE.
You've created a topic.
Test completed task
Click Check my progress to verify your performed task. If you have successfully created a Cloud Pub/Sub topic, you will see an assessment score.
Create a Pub/Sub topic.
Task 2. Add a subscription
Now you'll make a subscription to access the topic.
- Click Topics in the left panel to return to the Topics page. For the topic you just made click the three dot icon > Create subscription.
- In the Add subscription to topic dialog:
Type a name for the subscription, such as
MySub
Set the Delivery Type to Pull.
Leave all other options at the default values.
- Click Create.
Your subscription is listed in the Subscription list.
Test completed task
Click Check my progress to verify your performed task. If you have successfully created a subscription for Cloud Pub/Sub topic, you will see an assessment score.
Add a subscription.
Task 3. Test your understanding
Below are multiple choice questions to reinforce your understanding of this lab's concepts. Answer them to the best of your abilities.
A publisher application creates and sends messages to a ____. Subscriber applications create a ____ to a topic to receive messages from it.
subscription, subscription
topic, topic
topic, subscription
subscription, topic
Cloud Pub/Sub is an asynchronous messaging service designed to be highly reliable and scalable.
true
false
Task 4. Publish a message to the topic
Navigate back to pub/sub > Topics and open MyTopics page.
In the Topics details page, click Messages tab and then click Publish Message.
Enter
Hello World
in the Message field and click Publish.
Task 5. View the message
To view the message, use the subscription (MySub
) to pull the message (Hello World
) from the topic (MyTopic
).
- Enter the following command in Cloud Shell:
gcloud pubsub subscriptions pull --auto-ack MySub
Copied!content_copy
The message appears in the DATA field of the command output.
You created a Pub/Sub topic, published to the topic, created a subscription, then used the subscription to pull data from the topic.
Answer of Lab
gcloud pubsub topics create myTopic
gcloud pubsub subscriptions create --topic myTopic MySub