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.
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/09/04 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 2025/09/04 (UTC)."],[],[],null,["# Quickstart: Publish and receive messages in Pub/Sub by using the gcloud CLI\n\nPublish and receive messages in Pub/Sub by using the gcloud CLI\n===============================================================\n\nThis page shows you how to do the following operations in Pub/Sub using the Google Cloud CLI:\n\n- Create a topic and subscription.\n- Publish messages to the topic.\n- Receive messages from the subscription.\n\n*** ** * ** ***\n\nTo follow step-by-step guidance for this task directly in the\nGoogle Cloud console, click **Guide me**:\n\n[Guide me](https://console.cloud.google.com/freetrial?redirectPath=/?tutorial=pubsub_quickstart_gcloud)\n\n*** ** * ** ***\n\nBefore you begin\n----------------\n\n- Sign in to your Google Cloud account. If you're new to Google Cloud, [create an account](https://console.cloud.google.com/freetrial) to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.\n- Set up a Google Cloud console project.\n\n Set up a project \n Click to:\n - Create or select a project.\n - Enable the Pub/Sub API for that project.\n\n You can view and manage these resources at any time in the\n [Google Cloud console](https://console.cloud.google.com/).\n-\n [Install](/sdk/docs/install) the Google Cloud CLI.\n\n- If you're using an external identity provider (IdP), you must first\n [sign in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n\n-\n To [initialize](/sdk/docs/initializing) the gcloud CLI, run the following command:\n\n ```bash\n gcloud init\n ```\n | **Note:** You can run the gcloud CLI in the Google Cloud console without installing the Google Cloud CLI. To run the gcloud CLI in the Google Cloud console, [use\n | Cloud Shell](https://console.cloud.google.com/?cloudshell=true).\n\n- Set up a Google Cloud console project.\n\n Set up a project \n Click to:\n - Create or select a project.\n - Enable the Pub/Sub API for that project.\n\n You can view and manage these resources at any time in the\n [Google Cloud console](https://console.cloud.google.com/).\n-\n [Install](/sdk/docs/install) the Google Cloud CLI.\n\n- If you're using an external identity provider (IdP), you must first\n [sign in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n\n-\n To [initialize](/sdk/docs/initializing) the gcloud CLI, run the following command:\n\n ```bash\n gcloud init\n ```\n | **Note:** You can run the gcloud CLI in the Google Cloud console without installing the Google Cloud CLI. To run the gcloud CLI in the Google Cloud console, [use\n | Cloud Shell](https://console.cloud.google.com/?cloudshell=true).\n\n\u003cbr /\u003e\n\nCreate a topic\n--------------\n\nCreate a topic with the ID `my-topic`: \n\n```bash\ngcloud pubsub topics create my-topic\n```\n\nCreate a subscription\n---------------------\n\nCreate a subscription with the ID `my-sub` and attach it to `my-topic`: \n\n```bash\ngcloud pubsub subscriptions create my-sub --topic=my-topic\n```\n\nPublish messages\n----------------\n\nPublish a message to `my-topic`: \n\n```bash\ngcloud pubsub topics publish my-topic --message=\"hello\"\n```\n\nReceive messages\n----------------\n\nReceive the message from `my-sub`: \n\n```bash\ngcloud pubsub subscriptions pull my-sub --auto-ack\n```\n\nThe gcloud CLI prints the message to the command line.\n\nHow did it go?\n--------------\n\nIt worked! **Great!** What did you like about the quickstart? What could we have done better? [Let us know!](#).\nI got stuck. **We're sorry to hear that.** [Let us know what went wrong](#). We'll want to fix it.\n\nWhat's next\n-----------\n\n- See all the available\n [gCloud CLI commands](/sdk/gcloud/reference/pubsub) for Pub/Sub\n\n- Learn more about the Pub/Sub [concepts](/pubsub/docs/overview)\n discussed in this page.\n\n- Read the [basics of the Pub/Sub service](/pubsub/docs/pubsub-basics).\n\n- Work through an [end-to-end example](/pubsub/docs/building-pubsub-messaging-system)\n of a Pub/Sub system.\n\n- Try another Pub/Sub quickstart that\n uses [client libraries](/pubsub/docs/publish-receive-messages-client-library)\n or the [console](/pubsub/docs/publish-receive-messages-console).\n\n- Learn how to create [topics](/pubsub/docs/create-topic)\n and publish [messages](/pubsub/docs/publisher).\n\n- [Choose](/pubsub/docs/subscriber) a subscription type.\n\n- Learn more about [Pub/Sub APIs](/pubsub/docs/reference/service_apis_overview)."]]