5

i am using express.js and socket.io together for realtime push notification system. which are working fine, i share some line of code which i am using on server side

 var app = express();
 var server = require('http').Server(app);
 var io = require('socket.io')(server);
 server.listen(appconfig.webPort, function() {
  console.log('server runing at ' + appconfig.webPort);
 });

 io.on('connection', function(socket) {
   var id = setInterval(function() {
    some database logic here to get data from database  
       socket.emit(data);
  }, 5000);
 });

my question is does this code increase load on server ? as i get the data from database on every few seconds. if it is harmful , what is the best way of doing the same.

Meesam
  • 109
  • 1
  • 10

1 Answers1

2

my question is does this code increase load on server ? as i get the data from database on every few seconds. if it is harmful , what is the best way of doing the same.

Of course it does increase the load. For every socket.io connection that is made to your server, you are starting a separate 5 second interval timer and on every time that timer fires, you are running one or more database queries and then sending those results to that client.

If you had 50 connected clients, you'd be running these queries every 100ms (5000 / 50). If you had 1000 connected clients, the average interval would be every 5ms. Besides keeping your database pretty busy, your server would be pretty busy too. If you had thousands of connected clients, the scaling gets rapidly worse.

If you're getting the same data for every client, then you should change the way you're doing this. Make one query to the database every 5 seconds and then send the same results to all the clients. That scales much, much better.

If you can avoid polling your database at all and rely on some sort of event triggers to detect changes, that usually works even better.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • thanks , but the data is different for all users or client. how the facebook or any other application manage thier notification, since every user has its own notification.\ – Meesam Apr 03 '16 at 03:55
  • 1
    @Meesam - The key is to create a notification system on the server so when events that are relevant to a given user occur, they are just sent to that user and nothing is regularly polling a database. How to do that depends entirely on the application, the data storage structure, specific timeliness requiremeents and a bunch of other architectural things. In some cases the back-end storage has to be designed specifically to support the right kinds of notifications with no polling. It's regularly polling where 99% of the time there's nothing to find that kills scalability. – jfriend00 Apr 03 '16 at 03:59
  • :- if i am not wrong, you ask to create two kind of poliing, one is for dedicatedly to notification and the other for application fucationality. – Meesam Apr 03 '16 at 04:05
  • @Meesam - I'm not sure why you said that. Zero kinds of polling is the desired outcome, not two. – jfriend00 Apr 03 '16 at 04:15