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.
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.
Its very simple how to use it:
- Repository on github: https://github.com/mebjas/realtime-notification
- Todos in mebjas/realtime-notification: