How to Setup A Redis Pub/Sub System in Node.js to Make Communication Between Servers

Murat Gözel
Code with Benefit
Published in
3 min readJul 10, 2017
Use Redis As A Pub/Sub Messaging Service

There are variety of cases where your app needs separate servers to make certain group of operations. And mostly we want them to communicate each other to complete a certain task.

This article covers specific use case of pub/sub system which is csrf token usage that protects your forms against fake payloads in your frontend.

I have one frontend server which serves application views and one backend server which makes database operations and some other spesific tasks.

In general, a server generates a csrf token, injects it into the dom or saves it with a cookie and validates it when user send the form by reading it from a cookie or from a form input. This is how it works on a single server.

When you separate your code as two servers, backend server must know the csrf token you have generated in your frontend server in order to validate the user input comes from the user who access your frontend. This requires a communication channel between frontend and backend servers.

There are many server-to-server messaging libraries around like RabbitMQ, Kafka, ZeroMQ and even Redis. Even redis because it is not built for messaging but messaging one of its features. Node.js also has a built-in feature to send and receive messages between two processes.

Node.js built-in feature requires to open a child process to make messaging and this is not fit into my case. I haven’t used any messaging services mentioned above but used redis for different purposes. I look at its documentation and saw a pub/sub messaging system as a sub feature. So i decided to give it a try since i was already familiar with redis.

Pub/Sub simply describes two systems communicating with each other. Pub is publisher and sub is subscriber. There can be more than one publisher and subscriber in programs as you can see at the following in this article.

If you are new to redis, it’s a service that runs under specific port in your machine and you must install it first:

How to install redis on your machine?

It has a rich website with a lot of documentation: redis.io

Now you have redis running on your machine. Time to choose a node.js client. There are many libraries written to work with redis in node.js but node_redis and ioredis are only starred packages in the redis official website. I was sure both of them will do the job for us and i chose node_redis:

How to install redis node.js client in your project?

We have a backend server, frontend server, redis server and redis client. We are ready to setup messaging.

Frontend server in our case is publisher because it will send a fresh csrf token to the backend server in every request. And backend server is subscriber which listens for messages from frontend server and handles it.

Redis pub/sub system works through channels. Every message publisher sent must be sent by specifying channel(s). “parameter-mapping” is the name i found for my channel. Both publisher and subscriber will be messaging through this channel.

Sending a message is just a couple lines of code we put into the relevant section of our frontend server:

First argument of publish method is a channel name and second argument is our message that will be sent to the backend server is any type of string.

Listening and receiving messages is not harder than what we do in frontend server code. We’ll subscribe to a channel and listen for new messages:

As you can see in the code above, there is only one message listener across channels. So if we subscribe to more than one channel, we need to handle message by looking the channel it comes from.

This is a basic pub/sub system which is scalable as your application grows. Just create a new subscribers if you need to send a message to more than one server. It is also possible to increase publishers. This system will continue to work seamlessly as your application complexity grows.

--

--

Murat Gözel
Code with Benefit

Freethinker / Maker / Software Developer / Designer / Advertiser