Send response to all clients except sender Send response to all clients except sender javascript javascript

Send response to all clients except sender


Here is my list (updated for 1.0):

// sending to sender-client onlysocket.emit('message', "this is a test");// sending to all clients, include senderio.emit('message', "this is a test");// sending to all clients except sendersocket.broadcast.emit('message', "this is a test");// sending to all clients in 'game' room(channel) except sendersocket.broadcast.to('game').emit('message', 'nice game');// sending to all clients in 'game' room(channel), include senderio.in('game').emit('message', 'cool game');// sending to sender client, only if they are in 'game' room(channel)socket.to('game').emit('message', 'enjoy the game');// sending to all clients in namespace 'myNamespace', include senderio.of('myNamespace').emit('message', 'gg');// sending to individual socketidsocket.broadcast.to(socketid).emit('message', 'for your eyes only');// list socketidfor (var socketid in io.sockets.sockets) {} ORObject.keys(io.sockets.sockets).forEach((socketid) => {});


From the @LearnRPG answer but with 1.0:

 // send to current request socket client socket.emit('message', "this is a test"); // sending to all clients, include sender io.sockets.emit('message', "this is a test"); //still works //or io.emit('message', 'this is a test'); // sending to all clients except sender socket.broadcast.emit('message', "this is a test"); // sending to all clients in 'game' room(channel) except sender socket.broadcast.to('game').emit('message', 'nice game'); // sending to all clients in 'game' room(channel), include sender // docs says "simply use to or in when broadcasting or emitting" io.in('game').emit('message', 'cool game'); // sending to individual socketid, socketid is like a room socket.broadcast.to(socketid).emit('message', 'for your eyes only');

To answer @Crashalot comment, socketid comes from:

var io = require('socket.io')(server);io.on('connection', function(socket) { console.log(socket.id); })


Here is a more complete answer about what has changed from 0.9.x to 1.x.

 // send to current request socket client socket.emit('message', "this is a test");// Hasn't changed // sending to all clients, include sender io.sockets.emit('message', "this is a test"); // Old way, still compatible io.emit('message', 'this is a test');// New way, works only in 1.x // sending to all clients except sender socket.broadcast.emit('message', "this is a test");// Hasn't changed // sending to all clients in 'game' room(channel) except sender socket.broadcast.to('game').emit('message', 'nice game');// Hasn't changed // sending to all clients in 'game' room(channel), include sender io.sockets.in('game').emit('message', 'cool game');// Old way, DOES NOT WORK ANYMORE io.in('game').emit('message', 'cool game');// New way io.to('game').emit('message', 'cool game');// New way, "in" or "to" are the exact same: "And then simply use to or in (they are the same) when broadcasting or emitting:" from http://socket.io/docs/rooms-and-namespaces/ // sending to individual socketid, socketid is like a room io.sockets.socket(socketid).emit('message', 'for your eyes only');// Old way, DOES NOT WORK ANYMORE socket.broadcast.to(socketid).emit('message', 'for your eyes only');// New way

I wanted to edit the post of @soyuka but my edit was rejected by peer-review.