Understanding Cloud Pub/Sub by building a Pub/Sub system to update Cloud Firestore

Abhijeet Singh Verma
5 min readJun 30, 2020

Hi techies, In this blog, we’ll learn how to use Cloud Pub/Sub by creating a Pub/Sub system to update Firestore using the Google Cloud Pub/Sub service.

But before we get started, let us first have a look at the basic concepts we must be aware of:-

What is Cloud Pub/Sub?
Pub/sub is an asynchronous messaging service that decouples services that produce events from that process events.

  • Topic: A named resource to which messages are sent by publishers.
  • Subscription: A named resource representing the stream of messages from a single, specific topic, to be delivered to the subscribing application.
  • Message: The combination of data and attributes that a publisher sends to a topic and is eventually delivered to subscribers.

What is Cloud Firestore?
Cloud Firestore is a flexible, scalable database for mobile, web, and server development from Firebase and Google Cloud Platform. Like Firebase Realtime Database, it keeps your data in-sync across client apps through realtime listeners and offers offline support for mobile and web so you can build responsive apps that work regardless of network latency or Internet connectivity.

Now that we have an understanding of all the necessary components, let’s get started with the implementation steps.

Getting Started

Suppose we are having an online shopping website where we are using Cloud Firestore to keep track of orders. Our objective here is to decouple the service which is tracking the status of the order with the service responsible for updating the status of the order in the database (Cloud Firestore). And these two services will communicate with each other by sending messages through Pub/Sub. This will not only make our solution more robust but will also simplify the development process.

Setup

  1. Set up a Google Cloud Project or use an existing one.
  2. Enable the Pub/Sub API for the project.
  3. Create a service account with Pub/Sub Publisher, Pub/Sub Subscriber, and Cloud Datastore user role.
  4. Download a private key as JSON.
  5. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the path of the JSON file that contains the service account key.
  6. Install and initialize cloud SDK.

Create a Topic using Cloud SDK for publishing order status by using the following command.

gcloud pubsub topics create orders

Create a subscription using Cloud SDK for receiving order status messages by using the following command.

gcloud pubsub subscriptions create orders-sub --topic orders

Create a project and initialize it by running npm init command

Install the following client libraries

npm install --save @google-cloud/pubsub
npm install --save @google-cloud/firestore

Setup your database using Firestore

  1. Create a project on Firebase console or use an existing one.
  2. Click on Develop in the menu section and select Database.

3. Create a Cloud Firestore Database.

4. Start a collection named orders.

5. Add documents in the orders collection.

6. If you want you can also add more entries(documents) in the database.

Our database for our online shopping website is ready. We are now good to go with our services for tracking the status of the order and for updating the status of the order in the database.

Creating a Service for tracking the status of the order and notifying the service responsible to update the order status in the database using Cloud Pub/Sub publisher.

  1. Service A: Order Tracking Service (Responsible for publishing order status)
    Create a file named order-publisher.js: Here we will publish the current status of the order with order id using Cloud Pub/Sub. The code snippet given below will help us in publishing messages using Cloud Pub/Sub.

2.Service B: Order Status updation service (responsible for subscribing order status and updating the same in the database)
Create a file named order-subscriber.js: This service will subscribe to the messages published by service A, and whenever the message is received it will update the order status in the database. The code snippet given below will help us create a pull-based subscriber and how to update the Firestore document.

Execution:

  1. Before execution, set the environment variable GOOGLE_APPLICATION_CREDENTIAlS as mentioned in the setup above.
  2. Start the Service B using the command.
node order-subsciber.js

3. Start Service A in order to publish the status order 1 using the command.

node order-publisher.js
Order Status published by Service A
Service B subscribing the message published by Service A and updating order status in Firestore
And order status successfully updated in Firestore by Service B

Conclusion:
Here we have successfully decoupled the service which is tracking the status of the order with the service which is updating the status of the order in the database (Cloud Firestore), I hope the article helped you and developed a good understanding of how Cloud Pub/Sub works.

😊If you liked my blog and found it helpful, please hit the little clap 👏 and press the follow button. This will encourage me to bring to you more articles of this kind. If you have any questions, feel free to use the comment box :).

Thanks for reading😊.

--

--