How to use Google Cloud PubSub with a Node.js Application

Google Cloud Pub/Sub is a messaging service that allows applications to communicate with each other asynchronously. In this article, we will integrate Google Pub/Sub with an Express.js application, enabling us to publish and subscribe to messages seamlessly.



Prerequisites

Before we begin, ensure you have the following:

  1. Node.js installed on your machine.
  2. A Google Cloud account and a project set up.
  3. The Google Cloud SDK installed and configured.
  4. A service account with Pub/Sub permissions.
How to use Google Cloud PubSub with an Node.js Application - PrashantSuthar.com

Step 1: Set Up Google Cloud Pub/Sub

  1. Create a Google Cloud Project:
  2. Enable the Pub/Sub API:
    • Navigate to the APIs & Services dashboard.
    • Click on Enable APIs and Services.
    • Search for “Pub/Sub” and enable the API.
  3. Create a Service Account:
    • Go to the IAM & Admin section.
    • Select Service Accounts.
    • Click on Create Service Account, provide a name, and click Create.
    • Assign the Pub/Sub Admin role.
    • Click Done and generate a JSON key for your service account.
  4. Create a Pub/Sub Topic:
    • In the Pub/Sub section, click on Create Topic.
    • Name your topic (e.g., my-topic).
  5. Create a Subscription:
    • Select your topic and click on Create Subscription.
    • Name your subscription (e.g., my-subscription) and set the delivery type.

Step 2: Set Up Your Express.js Application

  • Initialize Your Express Application: Open your terminal and run the following commands:

mkdir express-pubsub
cd express-pubsub
npm init -y
npm install express @google-cloud/pubsub body-parser dotenv

  • Create Project Structure: Create the following files and directories:

touch index.js .env

  • Set Up Environment Variables: Open the .env file and add your Google Cloud project ID and the path to your service account key:

PROJECT_ID=your-project-id
KEY_FILE=path/to/your/service-account-file.json

How to use Google Cloud PubSub with an Node.js Application - PrashantSuthar.com

Step 3: Implementing the Express Application

Open index.js and add the following code:


require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const { PubSub } = require('@google-cloud/pubsub');

const app = express();
const pubSubClient = new PubSub({
    projectId: process.env.PROJECT_ID,
    keyFilename: process.env.KEY_FILE,
});

app.use(bodyParser.json());

// Publish message to Pub/Sub
app.post('/publish', async (req, res) => {
    const { message } = req.body;
    const topicName = 'my-topic';

    try {
        const dataBuffer = Buffer.from(message);
        await pubSubClient.topic(topicName).publish(dataBuffer);
        res.status(200).send('Message published.');
    } catch (error) {
        console.error('Error publishing message:', error);
        res.status(500).send('Error publishing message.');
    }
});

// Subscribe to messages
app.post('/subscribe', async (req, res) => {
    const subscriptionName = 'my-subscription';
    const subscription = pubSubClient.subscription(subscriptionName);

    const messageHandler = message => {
        console.log(`Received message: ${message.id}`);
        console.log(`Data: ${message.data}`);
        message.ack();
    };

    subscription.on('message', messageHandler);
    res.status(200).send('Listening for messages...');
});

// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
    console.log(`Server is running on port ${PORT}`);
});

Step 4: Testing with Postman

  • Start the Express Server: In your terminal, run:

node index.js

  • Publish a Message: Open Postman and create a new POST request to http://localhost:3000/publish. Set the body type to JSON and add:

{
    "message": "Hello, Pub/Sub!"
}

Click Send. You should see a response indicating that the message has been published.

  • Subscribe to Messages: Create another POST request to http://localhost:3000/subscribe. Click Send. You should see logs in the terminal indicating that the server is listening for messages.

Conclusion

You have successfully integrated Google Pub/Sub with an Express.js application. This setup allows your application to publish and subscribe to messages effectively. You can extend this implementation by adding error handling, message filtering, or integrating with other services.

This concludes the integration process. Feel free to explore further features of Google Pub/Sub to enhance your application!

I hope you like this article. Keep visiting my website for more upcoming articles if you need any help with How to use Google Cloud PubSub with a Node.js Application. you can contact me. You can ask me questions in the comments also. You can connect me on social media and the links are below in the footer section. Keep connected. Happy Coding.