ConnectionStateRecovery not working when switching from wifi to 4g #5248
-
Hi all, I'm trying to implement connection state recovery so that on instances of temporary network failure such as switching from wifi to 4g, the user will still receive the missed events during that time. However I'm bumping into the issue where upon reconnection, socket.recovered seems to be false, whereas I expect it to be true. I am using socket.io v4.7.4 with the in memory adaptor and options: Socket.io-client is v4.7.2 with options: What I'm seeing is that when I switch networks, on the client side it'll register 'disconnect' event with reason 'transport close' immediately. However, on server side the socket does not disconnect. Now when the network is successfully switched, client starts reconnecting with the same pid as the initial connection, which server receives in the 'connection' event. At this point, if I check However, from reading the documentation, it seems that this use case of switching networks should be covered by the connection state recovery feature. I've looked through the source code and it also seems to only add the connection state recovery for a socket upon the socket disconnection. Would anyone have any ideas of what might be going wrong and how to achieve the desired behaviour? Thanks a lot! Appreciate any input. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hi!
I think you are right, the server is not aware of the disconnection yet. What we could maybe do when restoring the session is to check if the ID exists, send some dummy packet over the connection to trigger the disconnection, and then try to restore the session. Source here: socket.io/packages/socket.io-adapter/lib/in-memory-adapter.ts Lines 445 to 448 in 7427109 What do you think? |
Beta Was this translation helpful? Give feedback.
Hmm yes you're right, I think we'd either have to store the relationship between
sid
andpid
, or loop through all the connected sockets.For clustered adapters like the one based on Redis, we would also have to make sure the session does not exist on another node.
If we have access to the
socket
object, we could listen to the "disc…