0

Implementing Redis Pub/Sub in Node.js Application

In this article, we will see how to implement redis pub sub in a node application.Implementing Redis Pub/Sub in Node.js Application

Change the way you create an object – Javascript weekly

Promises indide a loop – Javascript ES6

Prototypal Inheritance – Javascript

Before going into the implementation part, we will see what is pub sub and why we need it.

What is Pub/Sub

Pub/Sub is nothing but a publish subscribe model where subscriber subscribes to an event. subscriber notified when the publisher publish the event.

To explain it with a simple analogy. Let’s say you want to buy a latest iPhone in Amazon. But, due to a demand. it is sold out early. Amazon asks you to get notified when it is available.

In this place you are a subscriber subscribing to an event(when stock available). Amazon is the publisher(tells the stock is available).

When publisher(Amazon) publish an event(stock available). you(Subscriber) will get notified.

Implementing Pub/Sub

Publisher

we are using Redis PubSub which is a popular inMemory database. you can also use other pub sub models like Kafka,RabbitMQ etc.

Firstly, To implement Redis PubSub in Node.js application. you need to have redis installed on your machine. Secondly, you need to run the redis server in the command line.

After that, we need npm package called redis which connects with Express app with redis.

Mainly we are going to create three app servers. one is going to be a publisher and remaining two are subscribers.

npm init --yes
npm install express redis

create a file called server.js and add the following code.

const express = require('express');
const redis = require('redis');

const publisher = redis.createClient();

const app = express();

app.get('/',(req,res) => {
    const user = {
        id : "123456",
        name : "Davis"
    }

    publisher.publish("user-notify",JSON.stringify(user))
    res.send("Publishing an Event using Redis");
})

app.listen(3005,() => {
    console.log(`server is listening on PORT 3005`);
})

we are importing redis from package.

After that, we are creating a redis client to connect with redis server.In redis, we can connect any number of clients to the redis server.

Once we connect it, we can publish an event by calling publish method

publisher.publish("user-notify",JSON.stringify(user))

we need to pass the topic name to write the data and Data .

Subscribers

create two express servers with the following code in different folder.

server.js

const express = require('express');
const redis = require('redis');

const subscriber = redis.createClient();

const app = express();

subscriber.on("message",(channel,message) => {
    console.log("Received data :"+message);
})

subscriber.subscribe("user-notify");

app.get('/',(req,res) => {
    res.send("Subscriber One");
})

app.listen(3006,() => {
    console.log("server is listening to port 3006");
})

server.js

const express = require('express');
const redis = require('redis');

const subscriber = redis.createClient();

const app = express();

subscriber.on("message",(channel,message) => {
    console.log("Received data :"+message);
})


app.get('/',(req,res) => {
    res.send("subscriber two");
})

subscriber.subscribe("user-notify");

app.listen(3007,() => {
    console.log("server is listening to port 3007");
})

Now you can run both publisher and subscribers. when you run the publisher, the publisher will publish the data to subscribers.

Demo

That’s it for this article.Implementing Redis Pub/Sub in Node.js Application. To learn more about redis, you can watch a tutorial from Brad Traversy which is a great video.

Happy Coding 🙂

ganeshmani009

Leave a Reply

Your e-mail address will not be published. Required fields are marked *