Using Redis Pub/Sub with Node.js: It’s quite easy!

West Coast of Tidung Island, Kepulauan Seribu Regency, Jakarta, Indonesia

Overview

Pub/Sub is concept when some service is waiting for update from the event source. Usually the publisher is event source that emit some data then deliver it to its subcribers. The subcribers talk to message broker, for example Redis. Then once the publisher send the payload or message, the subscriber receive that message as soon as possible.

In this case, the publiser could be a backend service and the subscriber could be another backend service or a mobile application that develop by its native programming language, such as Android is developed by Java or iOS is developed by Objective-C. So rather than we use infinity loop and talk to the event source or maybe some database. We can wait the message to be delivered by Redis.

Prerequisite

Basically this project is require these things:

- Redis Server instance
- Node.js runtime
- NPM (Node Package Manager)
- Redis Client Library for Node.js

Then you can clone this repository: <url ke bitbucket> into your development device. Once the repository is cloned, within the directory please execute this command

npm install

That will install the dependency package for this project, thats defined in this package.json file:

{
“name”: “nodejs-pubsub-sample”,
“version”: “1.0.0”,
“description”: “nodejs pubsub example”,
“main”: “index.js”,
“scripts”: {
“test”: “echo \”Error: no test specified\” && exit 1"
},
“keywords”: [
“pubsub”,
“redis”
],
“author”: “ridwanbejo”,
“license”: “ISC”,
“dependencies”: {
“redis”: “².8.0”
}
}

How it Works?

When we run Redis Server on local machine, its become Redis Instance but is running locally. Then we can start publishing message to Redis and we could run another script to subscribe to certain channel on Redis Server.

For example here is the code for Publisher in Node.js:

var redis = require(“redis”);
var publisher = redis.createClient();
publisher.publish(“notification”, “{\”message\”:\”Hello world from Asgardian!\”}”, function(){
process.exit(0);
});

The code above use the publish method only from the redis client object and it only need three parameter. The first is the channel name, second is the message that we want to send, and last is nodejs callback that will execute after the message is sent.

Then here is the code for Subscriber in Node.js:

var redis = require(“redis”);
var subscriber = redis.createClient();
subscriber.on(“message”, function (channel, message) {
console.log(“Message: “ + message + “ on channel: “ + channel + “ is arrive!”);
});
subscriber.subscribe(“notification”);

The code above is listening to Redis once its run on the terminal or console. Whenever there a message that being sent from publisher, the subcsriber receive the message as soon as possible. And we can do everything on event ‘message’ whatever we want with the payload.

Of course the subscriber could be more than one subcriber that listen to one channel, also the publisher could be more than one publisher that send data to certain channel. So it must be very useful if you want to make interservice communication with event-based messaging concept. So you don’t have to make the service always perform request to another service.

Let’s run it!

First open console to run this command:

$ redis-server
Running Redis Server on Local Machine

Then open second console to run this command:

$ node subscriber.js
Subscribe the Consumer to “Notification” Channel

Finally open third console to start sending the payload to the subscriber:

$ node index.js
Execute the Producer to send the payload to “Notification” Channel

The publisher will be closed after it send the payload to Redis. But the subsriber is always running if we not halt the process by force.

In addition, you can open another console to make multiple subscriber for that channel.

Run several consumer that subcsribe to “Notification” channel

So this is just snippet. Whatever your backend was built, wether using Express.js, GraphQL, or using another programming language. As long its support Redis Client Library, we could built any publisher and subscriber that become your favorite choices.

Thanks for following this article. I hope you found it useful your projects.