I'm trying to make a simple chat server for friends/office which will at anytime have a maximum of 50 people connected.
Which of the below methods is good practice to broadcast messages in terms of efficiency and/or reliability?
Example Pipe Server:
var server = net.createServer(function(socket) {
for(var i=0; i<sockets_list.length; i++){
socket.pipe(sockets_list[i]);
sockets_list.pipe(socket);
}
sockets_list.push(socket);
});
Example Write Server:
var server = net.createServer(function(socket) {
sockets_list.push(socket);
socket.on('data', function(data){
for(var i=0; i<sockets_list.length; i++){
if(socket != sockets_list[i]){
sockets_list[i].write(data);
}
}
});
});
This pipe solution seems to be very wrong for at least three reasons:
.pipe
handles it somehow but I don't how and how reliable it is). You don't have to read/write every time data is available. In fact you should not. You should always consider a minimal time between consecutive reads/writes. It will be more efficient and secure (and having a lag for say half a second is not a big deal for a user, he won't even notice it).