CODEX

Easy real-time messaging system with Python and Nodejs (Redis Pub/Sub Tutorial)

Wasek Rahman
Mar 4 · 4 min read
Publish/Subscribe design pattern

You have an application in Python and another in Nodejs and you want them to exchange data in real-time. The obvious way would be to set up Websockets or a RestfulAPI and have them communicate through endpoints. This can be a very tricky and tedious task. So, what if I told you, you can do that with ease without setting up Websockets or a RestfulAPI?

For the code on github, please scroll to the end.

Introducing the Publish/Subscribe model

This is a pattern that allows many different applications to communicate with each other with ease despite of what programming language you are using. It is highly scalable and very easy to set up.

What is a Publish/Subscribe model?

The general idea is, there is a “channel” to which an application will send data, in other words publish data and another application will be subscribed to it, to receive data. Multiple applications can publish data to one channel and multiple applications can subscribe to one channel to receive data.

Getting Started

In this tutorial, I will be showing you how we can use Redis to utilize this pub/sub model. For this tutorial, I am assuming you already have a python and nodejs environment set up. All you now need is to

Alright then. Let’s get into it!

Run redis-cli.exe

First thing’s first. After we install redis, go to the installed folder and run redis-cli.exe. It should look something like this.

As you can see, it starts the redis server at port 6379.

Python Publisher

We are gonna set up our python publisher to make sure that we can send messages to a channel using python. First, we need to make sure we have redis for python installed.

pip install redis

Run the above command to install it. Now create a file called publisher.py and code in the following.

import redispublisher = redis.Redis(host = 'localhost', port = 6379)
message=""
channel = "test"
while(message!="exit"):
message = input("")
send_message = "Python : " + message
publisher.publish(channel, send_message)

That’s all! Our python publisher is set up successfully. What we are doing here is keeping the publisher running until we type in “exit”, so we can keep on sending messages to the channel. The channel the message will be sent to is called “test”. The channel can be anything you want.

Python Subscriber

Now we are going to set up our python subscriber. Create a file subscriber.py and code in the following.

import redissubscriber = redis.Redis(host = 'localhost', port = 6379)
channel = 'test'
p = subscriber.pubsub()
p.subscribe(channel)
while True:
message = p.get_message()
if message and not message['data'] == 1:
message = message['data'].decode('utf-8')
print(message)

We are subscribing to the channel ‘test’ , so that as soon as any message is sent to ‘test’, it will be received by our program here.

Now if you run the following codes on two separate terminals at the same time, we get something like this.

As soon as I input “hello” and “how are you?” in publisher.py, it gets picked up by subscriber.py in real time.

Now let’s get deeper and connect another program to this channel. This is where things get even more interesting!

Nodejs Publisher

We first need to set up a project in nodejs and install redis. Running these two commands does the trick!

npm init
npm install redis prompt-sync

We also installed prompt-sync as it is required by nodejs to ensure we can take user-input from the command-line.

After that’s set up, create a file called publisher.js and code the following

const redis = require("redis");
const publisher = redis.createClient();
const prompt = require("prompt-sync")({ sigint: true });
var channel = "test"
var message = prompt();
message = "Nodejs : " + message;
publisher.publish(channel, message, () => {
publisher.quit();
});

As you can see the channel we will publish our data to is called ‘test’.

Nodejs Subscriber

Now we are going to set up our nodejs subscriber. Create a file subscriber.py and code in the following.

var redis = require("redis");
var subscriber = redis.createClient();
var channel = "test";
subscriber.on("message", function (channel, message) {
console.log(message);
});
subscriber.subscribe(channel);

That’s all! We now have our nodejs subscriber ready to go.

Final Output

After we run our nodejs programs on separate terminals, our whole project looks like this.

That’s all for this tutorial. Do follow me and “clap” this writing if you guys learnt something cool and fun.

Here’s the whole code — https://github.com/WasekRahman/redis_pubsub_nodejs_python/
Please star it if you find it helpful.

Connect with me
Linkedin — https://www.linkedin.com/in/wasek-rahman/
Github — https://github.com/WasekRahman

CodeX

Everything connected with Code & Tech!

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store