Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joining same room more then once and clients in a room

I'm trying to figure out what happens if the clients emits to join the same room more then once, To test and find answer on this I wanted initially to find out how many clients room has after same clients send more then one emit for joining the room, but Rooms chapter in wiki https://github.com/Automattic/socket.io/wiki/Rooms is outdated. When I try to use "io.sockets.clients('room')" I get error "Object # has no method 'clients'".

So I got two questions: 1. what happens if client tries to join same room more then once? Will he get emits for that room for each time he has tried to join? 2. How can I find out which clients are in a room?

Im using socket.io v1.0.2

like image 398
Ismar Slomic Avatar asked May 29 '14 09:05

Ismar Slomic


1 Answers

I got an answer on this question at socket.io github.

  1. As per this line of code, the socket will receive emits only once. The socket is added to a room only once, and if another attempt is made for the same socket to join the room, this attempt will be ignored.

  2. There is currently no public API for getting the clients, and there is some discussion ongoing in #1428. If you really need to get them, for some reason, you can fetch the actual clients from the adapter, assuming you are not using the redis adapter like so:

    socket.join('test room');
    var clients = io.sockets.adapter.rooms['test room'];
    console.log(clients);
    for (var clientId in clients) {
      console.log(io.sockets.connected[clientId]);
    }
    
like image 125
Ismar Slomic Avatar answered Sep 28 '22 04:09

Ismar Slomic