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.

pubsub

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.

1npm init --yes
2npm install express redis

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

1const express = require("express")
2const redis = require("redis")
3
4const publisher = redis.createClient()
5
6const app = express()
7
8app.get("/", (req, res) => {
9 const user = {
10 id: "123456",
11 name: "Davis",
12 }
13
14 publisher.publish("user-notify", JSON.stringify(user))
15 res.send("Publishing an Event using Redis")
16})
17
18app.listen(3005, () => {
19 console.log(`server is listening on PORT 3005`)
20})

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

1publisher.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

1const express = require("express")
2const redis = require("redis")
3
4const subscriber = redis.createClient()
5
6const app = express()
7
8subscriber.on("message", (channel, message) => {
9 console.log("Received data :" + message)
10})
11
12subscriber.subscribe("user-notify")
13
14app.get("/", (req, res) => {
15 res.send("Subscriber One")
16})
17
18app.listen(3006, () => {
19 console.log("server is listening to port 3006")
20})

server.js

1const express = require("express")
2const redis = require("redis")
3
4const subscriber = redis.createClient()
5
6const app = express()
7
8subscriber.on("message", (channel, message) => {
9 console.log("Received data :" + message)
10})
11
12app.get("/", (req, res) => {
13 res.send("subscriber two")
14})
15
16subscriber.subscribe("user-notify")
17
18app.listen(3007, () => {
19 console.log("server is listening to port 3007")
20})

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

Demo

https://www.youtube.com/watch?v=sc1VY2AOewc

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.

https://www.youtube.com/watch?v=9S-mphgE5fA

Happy Coding :-)

Author

Hey, I’m Ganesh, Full stack engineer.I love to write technical content and help developers like me to grow in the industry. please consider supporting me.

To Read More

Building a Production-grade Nodejs,...

This article is the first part of building a production grade nodejs,graphql and...

Modern React Redux Tutorials with R...

This tutorial explain how you can build an application using modern react redux ...

Building a Piano with React Hooks

In this article, we will see how to build a piano with react hooks. Building a P...

TypeScript Basics - The Definitive ...

In this article, we will learn some basics of typescript which helps you to deve...

Here's why podman is more secured t...

In this article we will see about podman and why it is more secured way to run c...

Building a Production - Ready Node....

In this article, we will see how to build a Nodejs, TypeScript Application and d...

Nginx for Front-end Developers

This article is to explain Nginx for Front-end Developers in a much simpler way....

What is gRPC ? How to implement gRP...

Everyone talks about gRPC. Have you ever wonder how it works or how to implement...