I want to join clients from two rooms into a new room. Is there a way to achieve this? Like:
io.sockets.clients(room1 , room2).join(room 3);
var rooms = ['room1', 'room2', room3]; // list of rooms
socket.join('room1'); // assign the room on connection
var clients = io.sockets.clients(rooms[0]); // list of all clients in room1
for (client in clients)
{
clients[client].leave(rooms[0]); // leave room1
clients[client].join(rooms[2]); // join room3
}
Repeat the same for room2. Hope this helps
For people using socket.IO versions 1.0 or greater this may not work. Try the following code for the same.
var clients = io.sockets.adapter.rooms['Current Room Name'];
for (var clientId in clients ) {
//this is the socket of each client in the room.
var clientSocket = io.sockets.connected[clientId];
//If you want, you can remove the client form the first(current) room
//clientSocket.leave('current room');
clientSocket.join('new room to join');
}