socket.io
socket.io copied to clipboard
New Socket Connection establishes before existing disconnects on server.
Hi Team,
I have a usecase where i send userid in the query params while making the socket io connection to the server. and this userid is stored on the server so that any new socket request coming with same userid should not be allowed as long as the socket connection is open with this userid. This works fine.
But when using this on an android client device and switching mobile data off/on for a split second would initiate a new socket connection and the existing one is not closed at the server and server waits for pingTimeout + pingInterval before it detects the disconnect event on this socket. and since the existing one is not closed and a new socket request with same userid is made to the server, I reject this new connection. Ideally as soon as mobile data is switched off, the socket connection on both the client and server should be disconnected before making a new connection from the same client.
Please help me with this issue,
Thanks in Advance, Amit M
I got the same issue
For future readers:
I don't think there is much we can do here, in some rare cases the WebSocket connection is not properly closed, so the server becomes aware of the disconnection only once the hearbeat fails.
One could try to use io.in(userId).fetchSockets() to make sure there is only one single connection per userId.
Reference: https://socket.io/docs/v4/server-api/#serverfetchsockets
Please reopen if needed!