Powered By GitBook
Use Pub/Sub to send and receive real-time messages
Learn to use Pub/Sub to send and receive real-time messages.
Take the interactive version of this tutorial, which runs in the Cloud Console:
​​
​
​
</walkthrough-alt>

Introduction

Pub/Sub is a fully-managed real-time messaging service that allows you to send and receive messages between independent applications. This tutorial gives a brief introduction to the command-line interface for Pub/Sub, using the gcloud command-line tool.

Project setup

Google Cloud organizes resources into projects. This allows you to collect all of the related resources for a single application in one place.
Begin by creating a new project or selecting an existing project for this tutorial.
</walkthrough-project-setup>
For details, see Creating a project.

Open Cloud Shell

In this tutorial, you do much of your work in Cloud Shell, which is a built-in command-line tool for the Cloud Console.
Open Cloud Shell by clicking the </walkthrough-cloud-shell-icon>Activate Cloud Shell button in the navigation bar in the upper-right corner of the console.

Create your first topic

A topic is a named resource to which you send messages. Create your first topic with the following command:
1
gcloud pubsub topics create my-topic
Copied!

Add a subscription

To receive messages, you need to create subscriptions. A subscription needs to have a corresponding topic. Create your first subscription with the following command:
1
gcloud pubsub subscriptions \
2
create my-sub --topic my-topic \
3
--ack-deadline=60
Copied!
This command creates a subscription named my-sub attached to the topic my-topic. All of the messages published to my-topic will be delivered to this subscription.
The ack-deadline option sets an acknowledgement deadline of 60 seconds for this subscription. This deadline is explained in more detail later.

List topics and subscriptions

Before sending your first message, check whether the topic and the subscription have been successfully created.
List your topic and subscription using the following commands:
1
gcloud pubsub topics list
Copied!
1
gcloud pubsub subscriptions list
Copied!

Publish messages to the topic

Send two messages with the following commands:
1
gcloud pubsub topics publish my-topic --message hello
Copied!
1
gcloud pubsub topics publish my-topic --message goodbye
Copied!
Each of these commands sends a message. The first message is hello and the second is goodbye. When you successfully publish a message, you should see the messageId returned from the server. This is a unique ID automatically assigned by the server to each message.

Pull messages from the subscription

Now, pull the messages with the following command:
1
gcloud pubsub subscriptions \
2
pull --auto-ack --limit=2 my-sub
Copied!
This should return the two messages that you have just published. The messages have the data, hello and goodbye, as well as MESSAGE_ID. The MESSAGE_ID is a unique ID of the message that the server assigned.
Pub/Sub doesn't guarantee the order of the messages. It is also possible that only one message was returned; in that case, run the same command again until you see the other message.

Acknowledging messages

After you pull a message and process it, you must notify Pub/Sub that you successfully received the message. This action is called acknowledgement.
If you do not acknowledge the message before the acknowledgement deadline has passed, Pub/Sub will re-send the message.
The --auto-ack flag passed with the pull command automatically acknowledges a message when it is pulled.

Manual acknowledgement

Send a new message

Send a new message with the following command:
1
gcloud pubsub \
2
topics publish my-topic --message thanks
Copied!

Pull messages again

Pull the messages with this command:
1
gcloud pubsub subscriptions \
2
pull my-sub
Copied!
This should display the thanks message, as well as MESSAGE_ID and ACK_ID. The ACK_ID is an ID that you can use for acknowledging the message. Copy the ACK_ID value, which you will paste into an acknowledgement in the next step.

Acknowledge the message

Acknowledge the message with the following command, replacing [ACK_ID] with the ID that you copied in the previous step:
1
gcloud pubsub subscriptions ack \
2
my-sub --ack-ids [ACK_ID]
Copied!

See the topic and the subscription in the GCP Console

This concludes the gcloud command-line tutorial, but let's look at the web interface for the Cloud Console before finishing the tutorial.
Open the Navigation menu in the upper-left corner of the console, and select Pub/Sub.
</walkthrough-menu-navigation>
You can use this web interface to create and manage topics and subscriptions.

Clean up

Now that you have completed the tutorial, you should delete the resources that you created for the tutorial.
Select the checkbox next to the topic that you created, and click the Delete button to permanently delete the topic.

Conclusion

Congratulations!
</walkthrough-conclusion-trophy>
You have just walked through the basic concepts of Pub/Sub. The next step is to create your awesome applications!
For more information, see the Pub/Sub documentation and explore our code samples.
Last modified 7mo ago