Using socket.io is broadcasting the same efficiency as sending a message to every client

Viewed 1208

I want to optimize some code, a node server, sending messages to many clients. Right now it broadcasts to all clients even when it only needs to send messages to a fraction of them. There's no harm in this in terms of correctness, but the server is struggling to deal with so many messages in terms of efficiency. I'm considering writing some code to make sure it only sends to the clients that need it. However, I'm not sure if this is much more efficient or not.

Supposing that sending a message takes X computation. Suppose there are N clients connected. Does broadcasting then take N*X total computation? Or perhaps is broadcasting optimized so the computation is less? That's my question.

Just for the sake of defining what I mean by broadcast and single message, here's a bit of code. Note that this code is only to define the operations clearly:

var io = require('socket.io')(80);

io.on('connection', function (socket) {
  io.emit('this', { will: 'be received by everyone'}); // broadcast
});

io.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' }); // single message
});
1 Answers
Related