Integrating NodeJS with Google Cloud Pub/Sub

Introduction

Google Cloud Pub/Sub is a fully-managed, real-time messaging service that enables you to send and receive messages between independent applications. This article will guide you through integrating NodeJS with Google Cloud Pub/Sub for simple message sending and receiving.

Additionally, I've written an article on using Kafka, a distributed event streaming platform, to demonstrate message sending and receiving through NodeJS and Golang.

Prerequisites

Before proceeding with the following steps, make sure you have:

Implementing the Code

After setting up your NodeJS TypeScript project, install the following package:

yarn add @google-cloud/pubsub


Next, update the `main.ts` file with the following content:

import {PubSub, Topic, Subscription} from '@google-cloud/pubsub'

export const main = async (): Promise<void> => {
const projectId = 'project-id', // Google Cloud Platform project ID
topicName = 'topic-name', // Topic name
subscriptionName = 'subscription-name' // Subscription name
const pubsub = new PubSub({projectId})

const getTopic = async (topicName: string): Promise<Topic> => {
const topic = pubsub.topic(topicName)
const [exists] = await topic.exists()
if (exists) return topic
const [newTopic] = await topic.create()
return newTopic
}

const getSubscription = async (topic: Topic, subscriptionName: string): Promise<Subscription> => {
const subscription = topic.subscription(subscriptionName)
const [exists] = await subscription.exists()
if (exists) return subscription

const [newSubscription] = await topic.createSubscription(subscriptionName)
return newSubscription
}

const topic = await getTopic(topicName)
const subscription = await getSubscription(topic, subscriptionName)

subscription
.on('message', message => {
console.log('Received message:', message.data.toString())
})
.on('error', error => {
console.error('Error:', error)
process.exit(1)
})

topic.publishMessage({data: Buffer.from('Message ' + Date.now())})
}

main()

  • Replace the variables for project ID, topic name, and subscription name to match your setup.
  • In the `getTopic` and `getSubscription` functions, we've already implemented checks to see if the topic name and subscription name exist. This means you can execute the source code multiple times without encountering errors about the topic or subscription already existing.
  • The subscription will observe the topic, and when a message is published to the topic, the subscription will log the message content.


When I executed the code, I also published a message to the topic to verify it was working. You can also use the Google Pub/Sub Console to manually publish a message and achieve the same result.


Happy coding!

Comments

Popular posts from this blog

Kubernetes Practice Series

NodeJS Practice Series

Docker Practice Series

Deploying a NodeJS Server on Google Kubernetes Engine

React Practice Series

Setting up Kubernetes Dashboard with Kind

Sitemap

Using Kafka with Docker and NodeJS

A Handy Guide to Using Dynamic Import in JavaScript

Create API Gateway with fast-gateway