Implementing Redis Pub/Sub in Node.js Application
Tags: showdev, webdev, tutorial, beginners
Published: 2019-08-05 19:32:06

---
title: Implementing Redis Pub/Sub in Node.js Application
published: true
description: Implementing Redis Pub/Sub in Node.js Application
tags: showdev,webdev,tutorial,beginners
canonical_url: https://cloudnweb.dev/2019/08/implementing-redis-pub-sub-in-node-js-application/
---
<!-- wp:paragraph -->
<p>In this article, we will see how to implement redis pub sub in a node application.Implementing Redis Pub/Sub in Node.js Application</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><a href="https://cloudnweb.dev/2019/08/change-the-way-you-create-an-object-javascript-weekly/">Change the way you create an object - Javascript weekly</a></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><a href="https://cloudnweb.dev/2019/07/promises-inside-a-loop-javascript-es6/">Promises indide a loop - Javascript ES6</a></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><a href="https://cloudnweb.dev/2019/07/prototypal-inheritance-javascript-weekly/">Prototypal Inheritance - Javascript</a></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Before going into the implementation part, we will see what is pub sub and why we need it.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>What is Pub/Sub</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Pub/Sub is nothing but a publish subscribe model where subscriber subscribes to an event. subscriber notified when the publisher publish the event. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p> In this place you are a subscriber subscribing to an event(when stock available). Amazon is the publisher(tells the stock is available).</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>When publisher(Amazon) publish an event(stock available). you(Subscriber) will get notified.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":1177} -->
<figure class="wp-block-image"><img src="https://cloudnweb.dev/wp-content/uploads/2019/08/pubsub.png" alt="" class="wp-image-1177"/></figure>
<!-- /wp:image -->

<!-- wp:heading {"level":3} -->
<h3>Implementing Pub/Sub</h3>
<!-- /wp:heading -->

<!-- wp:heading {"level":4} -->
<h4>Publisher</h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>we are using Redis PubSub which is a popular inMemory database. you can also use other pub sub models like Kafka,RabbitMQ etc.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Firstly, To implement Redis PubSub in Node.js application. you need to have <a href="https://redis.io/topics/quickstart">redis </a>installed on your machine. Secondly, you need to run the redis server in the command line.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>After that, we need npm package called <strong>redis </strong>which connects with Express app with redis.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Mainly we are going to create three app servers. one is going to be a publisher and remaining two are subscribers.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>npm init --yes
npm install express redis</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>create a file called<strong> server.js</strong> and add the following code.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><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`);
})</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>we are importing redis from package. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Once we connect it, we can publish an event by calling <strong>publish </strong>method</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>publisher.publish("user-notify",JSON.stringify(user))</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>we need to pass the <strong>topic name</strong> to write the data and <strong>Data </strong>.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":4} -->
<h4>Subscribers</h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>create two express servers with the following code in different folder.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>server.js</strong></p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>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");
})</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p><strong>server.js</strong></p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>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");
})</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Now you can run both publisher and subscribers. when you run the publisher, the publisher will publish the data to subscribers.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":4} -->
<h4>Demo</h4>
<!-- /wp:heading -->

{% youtube sc1VY2AOewc %}

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

{% youtube 9S-mphgE5fA %}

<!-- wp:paragraph -->
<p>Happy Coding :-)</p>
<!-- /wp:paragraph -->