Quickstart using the gcloud command-line tool

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.

Before you begin

  1. Sign in to your Google Account.

    If you don't already have one, sign up for a new account.

  2. Set up a Cloud Console project.

    Set up a project

    Click to:

    • 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.

  3. Install and initialize the Cloud SDK.

Create a topic

Create a topic with the ID my-topic:

gcloud pubsub topics create my-topic

Create a subscription

Create a subscription with the ID my-sub and attach it to my-topic:

gcloud pubsub subscriptions create my-sub --topic=my-topic

Publish messages

Publish a message to my-topic:

gcloud pubsub topics publish my-topic --message="hello"

Receive messages

Receive the message from my-sub:

gcloud pubsub subscriptions pull my-sub --auto-ack

The gcloud tool prints the message to the command line.

How did it go?

What's next