You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Manager.socket() re-uses a manually disconnected socket without reconnecting it #1460
Describe the bug
When calling Manager.socket('/someNamespace') a second time after already having called it before, it will re-use the same socket. If that socket has been manually disconnected using Socket.disconnect(), it will be returned in a disconnected state.
To Reproduce
Please fill the following code example:
Socket.IO server version: 3.1.1
Server
const{ Server }=require('socket.io');constio=newServer(3000,{});io.of('/someNamespace').on('connection',(socket)=>{console.log(`connect ${socket.id}`);socket.on('disconnect',()=>{console.log(`disconnect ${socket.id}`);});});
Socket.IO client version: 3.1.1
Client
constio=require('socket.io-client');constmanager=newio.Manager('ws://localhost:3000');constfirstSocket=manager.socket('/someNamespace');firstSocket.on('connect',()=>{console.log('Connected first time');firstSocket.disconnect();console.log(firstSocket.connected);// falseconstsecondSocket=manager.socket('/someNamespace');secondSocket.on('connect',()=>{console.log('Connected second time');// Never happens});});
Expected behavior
I expected Manager.socket('/someNamespace') to return a socket that connects to the server. Either the old socket that tries to connect again, or a fresh socket.
Additional context
The context is a single page application where not all namespaces are relevant to all "pages" in the application, so we disconnect from a page-specific namespace when leaving a page. The reason for explicitly creating a Manager instance is to avoid creating a new underlying connection when we reconnect to the namespace.
Our current workaround is to manually call Socket.open() after Manager.socket().
Since we cannot have two (or more) active Socket instances for the same namespace and the same Manager, the most reasonable behavior would be to call Socket.connect() on the cached Socket instance, depending on the autoConnect option.
@darrachequesne I've noticed that the fix in b7dd891 causes another bug. If the socket uses multiplexing, the socket will emit two connection packets to the server. For example:
// Server receives the following connection packet *once*:// 0/oneletone=io.connect('/one');// Server receives the connection packet below *twice*:// 0/twolettwo=io.connect('/two');
This effectively creates two connected sockets to the /two namespace on the server. This leads to problems - which I encountered - when doing stuff on the server like
the two socket that was created on the client receives twofoo events beause there are actually two connected sockets on the server, but they are the same on the client. I will see if I can create a test case and perhaps a fix for it next week.
The issue is quite critical for me because it means that chat messages in a chat system of mine get delivered twice.
@sebamarynissen you are absolutely right, the fix did indeed contain a bug. This should be finally fixed by 46213a6, included in version 4.6.1. Could you please check?
Activity
darrachequesne commentedon Apr 7, 2021
Thanks, I could indeed reproduce.
Since we cannot have two (or more) active Socket instances for the same namespace and the same Manager, the most reasonable behavior would be to call
Socket.connect()on the cached Socket instance, depending on theautoConnectoption.What do you think?
Note: this behavior is present since forever: https://github.com/socketio/socket.io-client/blob/1.0.0/lib/manager.js#L278-L289
AntonNeld commentedon Apr 8, 2021
I agree, that sounds good.
thernstig commentedon Jan 23, 2023
@darrachequesne is this an easy fix?
fix: ensure manager.socket() returns an active socket
darrachequesne commentedon Feb 16, 2023
This should be fixed by b7dd891, included in version
4.6.0.Please reopen if needed!
sebamarynissen commentedon Feb 18, 2023
@darrachequesne I've noticed that the fix in b7dd891 causes another bug. If the socket uses multiplexing, the socket will emit two connection packets to the server. For example:
This effectively creates two connected sockets to the
/twonamespace on the server. This leads to problems - which I encountered - when doing stuff on the server likethe
twosocket that was created on the client receives twofooevents beause there are actually two connected sockets on the server, but they are the same on the client. I will see if I can create a test case and perhaps a fix for it next week.The issue is quite critical for me because it means that chat messages in a chat system of mine get delivered twice.
sebamarynissen commentedon Feb 18, 2023
I've been thinking a bit, and I think that the most elegant fix would be to only call
socket.connect()again ifSo
instead of how it's done now, being
fix: prevent duplicate connections when multiplexing
darrachequesne commentedon Feb 20, 2023
@sebamarynissen you are absolutely right, the fix did indeed contain a bug. This should be finally fixed by 46213a6, included in version
4.6.1. Could you please check?sebamarynissen commentedon Feb 21, 2023
@darrachequesne Checked and confirmed that
4.6.1fixes the bug. Thanks!