Yet another approach to realtime notifications in javascript

How do we implement notifications, realtime scores etc.
We would implement long polling method, use server sent events or since few years we have been using the web sockets. Advantage with the web sockets is, it maintains a persistent connection with server, cutting off all the overhead related to establishing the http connection again and again. A general approach to web socket based notification would be similar to long polling i.e.
1. We establish a connection
2. Server sends the data, and go to sleep for T seconds, and sends the data again and continue until the connection is broken.
To optimise this, we’d send data only when there was a change in data, otherwise silently go to sleep before checking again.

I’m not a huge fan of polling methods, so here’s an approach using Node JS, Redis and Socket.IO.

What we do here is subscribe to a redis variable for changes. So whenever the changes occur to a subscribed variable, Redis will notify Node server which will in turn notify the web client, and we can define actions for such notifcations. So in some way you are directly subscribing to a variable change in Redis from web client.
Kind of in, a -> b, b -> c so a -> c way.

realtime notification architecture

The advantages of this is:
1. No overhead at server.
2. You need to take care of updating the values in Redis Server only.
3. This approach can be used for realtime notifications, live scores, chat applications, realtime statistics etc.

I have implemented it, and created a javascript library for this. Its available on Github at mebjas/realtime-notification.
Its very simple how to use it:

Links:

Leave a Reply

Your email address will not be published. Required fields are marked *