This page shows you how to do the following operations in Pub/Sub using the
gcloud command-line tool:
- Create a topic and subscription.
- Publish messages to the topic.
- Receive messages from the subscription.
For step-by-step guidance on this task directly in Cloud Shell Editor, click Guide me:
The following sections take you through the same steps as clicking Guide me.
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
Set up a Cloud Console project.
- Create or select a project.
- Enable the Pub/Sub API for that project.
You can view and manage these resources at any time in the Cloud Console.
- Install and initialize the Cloud SDK.
Create a topic
Create a topic with the ID
gcloud pubsub topics create my-topic
Create a subscription
Create a subscription with the ID
my-sub and attach it to
gcloud pubsub subscriptions create my-sub --topic=my-topic
Publish a message to
gcloud pubsub topics publish my-topic --message="hello"
Receive the message from
gcloud pubsub subscriptions pull my-sub --auto-ack
gcloud tool prints the message to the command line.