socket.io emit to room will disconnect some clients - node.js

I am running a node.js server with socket.io 0.9. (xhr-polling)
I tried to simulate 300 clients connected to one big room.
I noticed that when I emit one single message to all members in the room, about 40-50 people in the room will be disconnected!
Syntax to emit message to everyone in the room:
io.sockets.in(roomId).emit("abc", "efg");
It is fine if I reduce the number of clients in the room to 100. Emit messages to room won't disconnect them.
This is definitely not acceptable. What is happening?

Related

How to find the room in Which the socket is in?

I want to know the room in Which the socket is in ? I want to broadcast to the other sockets in the room during the disconnect event
If you're only ever going to have a socket in one room at a time, you can just set that room as a property on the socket object when you join it to the room. socket.currentRoom = room. Then, upon disconnect, you can just access socket.currentRoom to see what room you had it in.
It is possible to dive into internal data structures to find out what rooms a socket is in, but if you're just using one room, then the socket.currentRoom is probably easiest.

are socket io rooms automaticaly destroyed if empty?

Im starting out with node.js and socket.io.
I have 2 questions:
- When a room is empty, is it automatically destoyed unbtil recreated?
and if it is not destroyed automaticaly, does it take up much ressources on the server?
on the server side. is it the io server or the connected socket that should transmit the data?
socket.emit('doSomething');
or
io.emit('doSomething');
The room is automatically removed from the array and the nodeJSs' V8 Garbage Collector finishes the job of completely removing the room from ram. You don't have to worry about any of that. Remember that all users are automatically put on a room on joining the server ( the socket.id named room ). io.emit should be used when you want to send a message from the server to anyone and socket.emit should be used when you want to send a message only to the sender. More information can be found on this answer: https://stackoverflow.com/a/40829919/7868639

Socket IO detect if user leaves a room

I want to listen for the event in which a user leaves a room. From what I've researched, socket.rooms apparently contains the rooms that a socket is in. However, when I listen for the disconnect event from a socket, apparently the socket left the room prior to disconnect. Therefore, socket.rooms will yield an empty object after the disconnect event. I need to listen specifically for the event in which a user leaves a room, not the socket disconnect event, since I have an array of room objects and wish to delete the user from the room when they leave.
A user cannot leave a room without your server removing them from the room or when the user disconnects. Clients cannot leave a room on their own. So, to know when a user leaves a room, you just need to hook into your own code that removes them from a room and also listen for the disconnect event.
Other than a disconnect, the only other way a user can leave a room is if your own code removes them from a room so you can just hook into that specific function and trigger the update of your own data structures when your own code removes them from the chat room.
For the disconnect event, if you are maintaining your own room data structures, then you can just remove a given socket from any room that you find it in when you get the disconnect event (e.g. search each room and remove it from any room that you find that socket in).

Socket.io - nodejs server side. transfer close after reconnection

I have two browser socket.io clients. For example. Client A and Client B and They are connected to Room one.
Following behavior is happening on my computer (installed nodejs 5, socket.io 1.4.5):
We suppose that both of clients are connected to Room One. When Client A is disconnected (lose connection) and reconnect after timeout and Client B does not use emit for send message to Room one, then original socket of Client A will be closed with transfer reason: timeouted. Thats good for me. I need it this behavior.
On other side:
When Client A is disconnected (lose connection) and reconnect in timeout time and Client B during that disconnected time uses emit for send message to Room one, then original socket of Client A after reconnect will be immediately closed with transfer reason: closed and not timeouted.
It looks that there is some behavior that selects disconnected sockets in timeout time for that some other online socket try emit message to same room. When emited message cannot delivery to disconnected socket (on client side), then this socket is probably select for immediately close of transaction after reconnect in timeout time.
I noticed futher issue. When mobile client lost connecting and somebody send emit to room where mobile client was connected, then mobile socket is disconnected immediately without timeout with reason transport close on linux.
the situation above is different for mac system. The socket is disconnected either timeout is gone or immediately if client mobile is reconnected.
Can anybody help what is a reason that this behaviors on same version of node (6.2) and same version of socket (1.4.6) are different?

Keep socket.io room alive when all clients disconnect?

I have a multiplayer game with game rooms built in Node.js and socket.io. There are 4 players per game room and rooms are created when the first player joins. When I send data to a client connected to the room, I always perform the following check to make sure they're still in the room (sock_id is the socket ID of the client):
if(io.sockets.manager.rooms['/' + room].indexOf(sock_id) > -1){
...
}
If a player leaves the room, he is replaced by a robot player. However, I'm running into a problem when all 4 players disconnect. The room is automatically destroyed, and the above check throws an error cannot call method indexOf of undefined, indicating the room doesn't exist. One way to counter this is to add a check to make sure the room exists to every such check on the Node server (and there are quite a few).
But, I was wondering if it's possible to keep the room alive in some way even when all clients disconnect. I could then destroy the room when the game in question ends, and the problem is solved.

Resources