This demo was created for my talk on "Building Scalable Cloud Applications with Pub/Sub Messaging during the GDG Cloud Conversations Event in Bengaluru (India) on 10th February 2024.
- Install the dependencies (there is only one:
@google-cloud/pubsub
)
npm install
- This project assumes that you have
gcloud
CLI set up in your terminal with the correct credentials.
In order to use this script, you will first have to create a topic and one or two subscriptions. Here are the steps:
- Visit the "Pub/Sub" page in GCP.
- Click on "Create Topic" and enter the topic name "test-topic". Leave all other options as default and click "Create".
- This will create a topic named "test-topic" and a subscription named "test-topic-sub".
At this point, you can run your first demo.
- Open a terminal window and run
npm run sub
. This will launch the subscriber script which pulls from the "test-topic-sub" subscription. - Open another terminal window and run
npm run pub
. This will publish a message on the "test-topic" topic.
Result: you should be able to see the message in your subscriber terminal window.
Next, we will demo the use of multiple subscribers on the same subscription.
- Open another terminal and run
npm run sub
in it. - On the publisher terminal window, run
npm run pub
again.
Result: You will only see the message on one of the two open subscriber terminal windows.
Reason: This is because when one "worker" on the "test-topic-sub" subscription ACKs the message, it is deleted from Pub/Sub so that multiple workers do not perform duplicate processing on the same message.
Now, we will demo the use of multiple subscribers on different subscriptions.
- In your Google Cloud Console, on the topic page, click on the "Create subscription" button.
- Add "diffSub" to the Subscription ID field. Create everything else as default and click on the "Create" button.
- This will create a new subscription named "diffSub"
- Open another terminal window and run
npm run sub2
. A third subscriber will start in this window. - Go back to the publisher terminal window and run
npm run pub
again.
Result: You will the message on the first and the last subscriber terminal windows, i.e. on one worker for each subscription.
Reason: When you have multiple subscriptions on the same topic, the message will have to be ACKed from all the subscriptions before it is deleted. This ensures that different workers can be act on messages on the same topic without duplications in the same subscription.