Setup Google Cloud PubSub

Jul 17, 2019

About Cloud Pub/Sub

  • Pricing: Free 10 GB of data volume per month. Notice there are message storage fees of $0.27 per GiB-month.
  • Currently I am only using one publisher and one subscriber

Create a topic

Assuming you already have a Google Cloud account and setup gcloud.

via Cloud Console

Visit Google Cloud Console -> Pub/Sub Topics -> Create Topic.

NOTE: Visiting this page will enable Cloud Pub/Sub API.

Enter a topic name and click Create Topic.

NOTE: A topic forwards messages from publishers to subscribers.

NOTE: Topic ID - projects/PROJECT_ID/topics/TOPIC_NAME

via gcloud

gcloud pubsub topics create TOPIC_NAME --project PROJECT_ID

via Python API

Create Cloud PubSub Topic and Subscription via Python API

Create a subscription

  • Each subscription should have only 1 subscriber.
  • If there are 2 subscribers, the messages will be distributed evenly among each subscriber (Message1 -> Subscriber1, Message2 -> Subscriber2, Message3 -> Subscriber1, ...).
  • If you have multiple subscribers, you should create multiple subscriptions for the same topic.

via Cloud Console

Click of 3-vertical-dot of the topic and click Create subscription

  • Subscription name
  • Delivery type: Pull (a listening clent) or Push (to specific URL endpoint)
  • Subscription expiry: Pub/Sub can delete inactive subscriptions automatically. Configure the minimum required time of inactivity to schedule a subscription for deletion. This time must be longer than the message retention duration.
  • Acknowledgement deadline: How long Pub/Sub waits for the subscriber to acknowledge receipt before resending the message
  • Message retention duration

NOTE: I am using a Delivery type = Pull.

via gcloud

gcloud pubsub subscriptions create SUBSCRIPTION_NAME --topic TOPIC_NAME --project PROJECT_ID

via Python API

Create Cloud PubSub Topic and Subscription via Python API

Test

At Google Cloud Console -> PubSub -> Topics, click on the Topic name. Click Public Message, enter Message body and Message attributes then click publish.

To pull the message, run

gcloud pubsub subscriptions pull --auto-ack SUBSCRIPTION_NAME --project PROJECT_ID

Python Client

Refer to Setup Google Cloud PubSub (Python CLI).

References:

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.