wilsonpage
wilsonpage

Reputation: 17610

Socket IO V0.7: How to send message to more that one specific client

In version 0.6 I used this method to send a message to specific group of clients. It will exclude the clients with the session IDs in the 'excludedClients' array and send to all clients who are not excluded.

var excludedClients = [sessionID1, sessionID2, sessionID3];
io.broadcast(msg, excludedClients);
  1. Is this method still applicable in V0.7?
  2. Is this the best way of doing this?

Hope someone can give me a hand with this, there doesn't seem to be any solid documentation for Socket IO anywhere, only briefly explained examples on the github page.

Upvotes: 1

Views: 2135

Answers (1)

Michelle Tilley
Michelle Tilley

Reputation: 159105

  1. I believe so; check out the wiki page on 0.6 to 0.7 migration.

  2. If your clients can be grouped logically, you can use rooms.

Rooms

Sometimes you want to put a bunch of sockets in one room, and send a message to them. You can leverage rooms by calling join on a socket, and then with the flags to and in:

io.sockets.on('connection', function (socket) {
  socket.join('a room');
  socket.broadcast.to('a room').send('im here');
  io.sockets.in('some other room').emit('hi');
});

Upvotes: 4

Related Questions