Clients get disconnected automatically

Problem

I am trying to implement a chat module for my app and getting 2 problems.

The first one is that my clients in sockets keep on disconnecting automatically. Here's what I am doing.

My clients get connected in a room which is dynamically created and the name of the room is a random ID that i generate.Now when a client sends a message I print a log to see how many people are there in the room and it logs "2" on the server side which is correct.But when I keep on sending messages from the clients to the server it start showing me "1" client is connected and after some time it shows me 0 clients are connected in the room.The people are automatically disconnecting why is this happening?

socket.on('SendChat',function(msgobj){      //pass the message to all people in the room.
console.log("Msg from" + msgobj.MsgSenderName + " in RoomID = "+ msgobj.RoomID);
console.log("People in room " +io.sockets.clients(msgobj.RoomID).length);
    socket.broadcast.to(String(msgobj.RoomID)).emit('RecieveChat',msgobj);  
});

This event is raised on the server side when someone sends a message from any of the client and the msgobj will contain the RoomID.So you can see I am logging number of people in the room.

The second problem is I am trying to broadcast the message which I recieve from a client.But the event at the other client is not raised.In the above code you can see the last line I used to broadcast msg to all the other clients but the events at those clients is not fired I don't know why.Here's what is on my client

this.Socket.on('RecieveChat',function(obj){
    self.Controller.RecieveChat(obj);
});

and here's my log

enter image description here You can see the RoomID is same but the people in room are leaving and this snapshot is only showing 1 people in room before that there were two people in room which then cut of to 1 and eventually 0.

EDIT:

When I change socket.broadcast.to(String(msgobj.RoomID)).emit('RecieveChat',msgobj); to socket.broadcast.emit('RecieveChat',msgobj); it starts working but not working if I emit inside a room using the to method.

Transport end (close timeout) this seems to be causing the issue.How to resolve this?

Problem courtesy of: Mj1992

Solution

Finally found the solution here the problem was with the version of socket.io that I was using.

Solution courtesy of: Mj1992

Discussion

There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.