1

I am trying to put together an app based on chat rooms where each room can only have 2 people in the room.

It also needs to remember the order in which the users entered the room. It is to extend on a chatroom type socket example but allow the use of "rooms" for pairs of people who can then play "the random game" i.e.there will be a "lobby" of rooms or "create a room" and each room will either be 1 person in or 2 people (and full).

What I cannot seem to figure out is how to limit the number of people connecting to a room in socket.io?

It is possible?

Mike
  • 58
  • 2
  • 10
  • 1
    It's not a socket.io base thing. This is something you have to implement yourself. Show some code here of what you already have to get some specific pointers. – Zlatko Feb 14 '14 at 01:45

1 Answers1

3

You have to implement your own logic. However, you can count numbers of clients in a room by this:

io.sockets.clients('room')

This will return an array of Socket instances of all clients in the room. Check if the length of this array is greater or less than 2. If a new client wants to join a room which already has 2 client then you can easily prevent him from doing this. Hope this would work for you.

M Omayr
  • 1,351
  • 1
  • 9
  • 19
  • 2
    It is not valid for 1.X. For that refer to http://stackoverflow.com/questions/9352549/getting-how-many-people-are-in-a-chat-room-in-socket-io#24425207 – Musa Haidari Jun 07 '16 at 05:53