Realtime notifications using nodejs, and redis
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


If you are making a web application which needs to update client in realtime, you can use this example to quickly write such an application in nodejs using Redis pubsub is used as a data source in this example. You can use any other data source.

Since these kind of applications are event-driven on server side (for example, a new message arrived for user) it is a good idea to use nodejs. is used to maintain connection with the user. It uses websockets if available else uses fallback machanisms like long-polling. For more details see


  • redis
  • nodejs
  • nodejs redis module
  • nodejs module


For installing redis visit

For installing nodejs visit

If you are on linux, you can use package manager of your distribution to install redis and nodejs.

Use npm to install nodejs dependencies

npm install redis

You may want to use hiredis on production as explained on project page


Start the server by

cd server
sudo nodejs server.js

Now hit http://localhost on your browser

Server side flow

When the server starts, an http handler is created whose only task currently is to server index.html.

There is handler which listens to all the websocket connections and manages them. It subscribes them to appropricate rooms and sends updates.

Lastly there are two redis clients one of which subscribes to channels on redis and as soon as a message is received, it broadcasts them in appropricate rooms. The second client simply publishes dummy updates to redis channels to which the first redis client is subscribed to.

Client side flow

As soon as the page load is done, client tries to connect to the server and on connection sends subscribe message for a particular channel. When updates are received it displays them

I have documented the code as extensively as I could. Please email me in case of doubts.