GCP PubSub

Google Cloud PubSub is an asynchronous global messaging service. There are three key terms in PubSub: topics, publishing, and subscribing.

  • A topic is a shared string that allows applications to connect with one another through a common thread.
  • Publishers push (or publish) a message to a Cloud PubSub topic.
  • Subscribers make a “subscription” to a topic where they will either pull messages from the subscription or configure webhooks for push subscriptions. Every subscriber must acknowledge each message within a configurable window of time.

In sum, a producer publishes messages to a topic and a consumer creates a subscription to a topic to receive messages from it.

Basic Commands

  • gcloud pubsub topics create <topic-name> Create new topic with the specified name.
  • gcloud pubsub topics list --uri List current topics (--uri flag only shows list of resources rather than full output).
  • gcloud pubsub topics delete <topic-name> Deletes the specified topic.
  • gcloud pubsub topics list-subscriptions <topic-name> List subscriptions specific to a topic.
  • gcloud pubsub subscriptions create --topic <topic-name> <subscription-name> Creates a new subscription with the specified name on the specified topic (topic must pre-exist).
  • gcloud pubsub subscriptions list --uri List all subscriptions (see --uri flag above).
  • gcloud pubsub subscriptions delete <subscription-name> Delete the specified subscription.

Publishing and Consuming Messages

  • gcloud pubsub topics publish <topic-name> --message "Hello" Publish the following message to the specified topic.
  • gcloud pubsub subscriptions pull <subscription-name> --auto-ack --limit=<number> Pull/consume the number of messages specified in the --limit flag from the specified subscription.

© Filip Niklas 2024. All poetry rights reserved. Permission is hereby granted to freely copy and use notes about programming and any code.