Using socket.IO to build a real-time notification system

M Rezaur Rahman
Nov 6, 2016 · 2 min read

SocketIO has become a popular day by day for it’s real time bidirectional communication. Instead of using a time interval based checking, we can use socket based communication which is more efficient in terms of using network bandwidth. It’s extremely easy to build a chat client, analytics, collaborative editing, streaming and lots of real time activities by using socketIO.

Here I’m describing how socketIO can be used to make a notification system. I’m using nodeJs here. In order to install socketIO, we can use this command:

npm insatall — save socket.io

I’m using an express app here. If you use a different framework, your mileage may vary.

In my app, the main javascript file is server.js, I’m using it as my bootstrapping file. Usually in most of the examples the implementation is shown in a single file, which is not scalable. I’m using modular approach to implement socketIO which is easier to maintain and not painful to debug.

In the server.js file, I added the following lines:

From that lines of code, we can see, I’m using a receiver helper module which contains the socket related methods. And on the other hand, the socketIO helper module is being used as accessor module.

Here is the code of socketio.js accessors.

Here is a sample of receivers module.

You can use any string value instead of ‘generated notification’ and the second parameter of the emit method would be the text of the notification message.

In order to get the notification value and send back response from the client side, this can be a way around.

In the html <body> tag, use the following line:

This is the simplest way to use socketIO for building notification system. You can write different logic for notification authorization, classification and customization.

Please ask me any question at @mrezaurrahman twitter handler.

Jeeon

Make Wellbeing Universal

M Rezaur Rahman

Written by

Code, books, science and Apple products. 💻📚🧪🔭🍎

Jeeon

Jeeon

Make Wellbeing Universal