# Publish and receive messages in Pub/Sub by using the gcloud CLI

This page shows you how to do the following operations in Pub/Sub using the Google Cloud CLI:

- Create a topic and subscription.
- Publish messages to the topic.
- Receive messages from the subscription.

*** ** * ** ***

To follow step-by-step guidance for this task directly in the
Google Cloud console, click **Guide me**:

[Guide me](https://console.cloud.google.com/?tutorial=pubsub_quickstart_gcloud)

*** ** * ** ***

## Before you begin

Complete the following steps before running your quickstart.

### Set up your project

<br />

### Required roles

To complete this quickstart, you need the following Identity and Access Management (IAM)
(IAM) roles.


To get the permissions that
you need to complete this quickstart,

ask your administrator to grant you the
[Pub/Sub Editor](https://docs.cloud.google.com/iam/docs/roles-permissions/pubsub.md.txt#pubsub.editor) (`roles/pubsub.editor`) IAM role on your project.


For more information about granting roles, see [Manage access to projects, folders, and organizations](https://docs.cloud.google.com/iam/docs/granting-changing-revoking-access.md.txt).


You might also be able to get
the required permissions through [custom
roles](https://docs.cloud.google.com/iam/docs/creating-custom-roles) or other [predefined
roles](https://docs.cloud.google.com/iam/docs/roles-overview#predefined).

## Create a topic

Create a topic with the ID `my-topic`:

```bash
gcloud pubsub topics create my-topic
```

## Create a subscription

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

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

## Publish messages

Publish a message to `my-topic`:

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

## Receive messages

Receive the message from `my-sub`:

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

The gcloud CLI prints the message to the command line.

## How did it go?

<button class="button button-blue expand-control gc-analytics-event" data-category="Quickstart" data-action="Feedback" data-label="Pub/Sub API, Client Libraries" data-value="1" track-type="quickstart" track-name="feedbackLink" track-metadata-position="steps" track-metadata-end-goal="itWorked"> It worked! </button>

> [!TIP]
> **Great!** What did you like about the quickstart? What could we have done better? [Let us know!](https://docs.cloud.google.com/pubsub/docs/publish-receive-messages-gcloud.md.txt#).

<button class="button expand-control gc-analytics-event" data-category="Quickstart" data-action="Feedback" data-label="Pub/Sub API, Client Libraries" data-value="0" style="position: absolute; top: 0; left: 110px;" track-type="quickstart" track-name="feedbackLink" track-metadata-position="steps" track-metadata-end-goal="itFailed"> I got stuck. </button>

> [!WARNING]
> **We're sorry to hear that.** [Let us know what went wrong](https://docs.cloud.google.com/pubsub/docs/publish-receive-messages-gcloud.md.txt#). We'll want to fix it.

## What's next

- See all the available
  [gcloud CLI commands](https://docs.cloud.google.com/sdk/gcloud/reference/pubsub) for Pub/Sub

- Learn more about the Pub/Sub [concepts](https://docs.cloud.google.com/pubsub/docs/overview.md.txt)
  discussed in this page.

- Read the [basics of the Pub/Sub service](https://docs.cloud.google.com/pubsub/docs/pubsub-basics.md.txt).

- Work through an [end-to-end example](https://docs.cloud.google.com/pubsub/docs/building-pubsub-messaging-system.md.txt)
  of a Pub/Sub system.

- Try another Pub/Sub quickstart that
  uses [client libraries](https://docs.cloud.google.com/pubsub/docs/publish-receive-messages-client-library.md.txt)
  or the [console](https://docs.cloud.google.com/pubsub/docs/publish-receive-messages-console.md.txt).

- Learn how to create [topics](https://docs.cloud.google.com/pubsub/docs/create-topic.md.txt)
  and publish [messages](https://docs.cloud.google.com/pubsub/docs/publisher.md.txt).

- [Choose](https://docs.cloud.google.com/pubsub/docs/subscriber.md.txt) a subscription type.

- Learn more about [Pub/Sub APIs](https://docs.cloud.google.com/pubsub/docs/reference/service_apis_overview.md.txt).