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
Hello! I'm working on implementing reconnection logic for my project and have some questions about the reliability of disconnect reasons.
My server maintains state in two main objects:
// Users mapped by socket ID
public users: { [socketId: string]: { username: string, inRoom?: string } };
// Rooms containing user info, scores, etc.
public rooms: { [roomId: string]: RoomInfo };
🔮 Currently, I clean up all user state on any disconnection. But this created a challenge, cos on reconnection I would have lost everything. So, in order to make reconnection scenario NOT DISTRUPTIVE.. i was thinking to to distinguish between intentional disconnects and connection issues using the disconnect reason:
socket.on('disconnect', (reason) => {
if (reason === 'transport close' || reason === 'ping timeout') {
// Connection issues - delay cleanup
// SAVE USER disconnection state and a timestamp.
// Have a timer running in the server(every 30s like) and clean state of who was disconnected for more than 2 min(max caching of socket)
} else {
// Intentional disconnect - immediate cleanup
this.cleanup(socket.id);
}
});
On reconnection I would clean the disconnection state so their state doesn't get cleaned up
In this way I only remove the user state if they are actually leaving, but keep everything for a little while if looks like a connection issue. What do you think ?🤔 This should make the "restoration" not a problem?
Are these disconnect reasons reliable for distinguishing between intentional disconnects and connection issues?
If I delay cleanup for connection issues, will the socket.id remain stable during reconnection within the maxDisconnectionDuration window?
Will missed events be automatically replayed to the client upon successful reconnection?
🔮 Any guidance is appreciated.. and ask me any question. Overall Goal:
I'm building a multiplayer game where users join rooms and play together. When a user temporarily disconnects (e.g., poor connection), I want to:
Keep their game state alive for a reconnection window
Allow them to seamlessly rejoin their game session
Receive any missed game events
Maintain their room position and game progress
Rather than implementing complex state restoration, I'm hoping to leverage Socket.IO's built-in recovery mechanisms by delaying cleanup for unintentional disconnects.
🔮 Lastly How can the reconnection scenario be automatically tested?
I am using jest for testing. Manually I was simply using Chrome dev tool and setting the throttling to offline
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello! I'm working on implementing reconnection logic for my project and have some questions about the reliability of disconnect reasons.
My server maintains state in two main objects:
// Users mapped by socket ID
public users: { [socketId: string]: { username: string, inRoom?: string } };
// Rooms containing user info, scores, etc.
public rooms: { [roomId: string]: RoomInfo };
🔮 Currently, I clean up all user state on any disconnection. But this created a challenge, cos on reconnection I would have lost everything. So, in order to make reconnection scenario NOT DISTRUPTIVE.. i was thinking to to distinguish between intentional disconnects and connection issues using the disconnect reason:
On reconnection I would clean the disconnection state so their state doesn't get cleaned up
In this way I only remove the user state if they are actually leaving, but keep everything for a little while if looks like a connection issue. What do you think ?🤔 This should make the "restoration" not a problem?
I'm trying to avoid complex state restoration by keeping state alive during the reconnection window. Is this an efficient approach?
If not please let me know!
I have also had a look at the documentation:
https://socket.io/docs/v4/connection-state-recovery
https://socket.io/docs/v4/tutorial/step-6
https://socket.io/docs/v4/tutorial/handling-disconnections
🔮** Extra Questions:**
maxDisconnectionDuration
window?🔮 Any guidance is appreciated.. and ask me any question.
Overall Goal:
I'm building a multiplayer game where users join rooms and play together. When a user temporarily disconnects (e.g., poor connection), I want to:
Rather than implementing complex state restoration, I'm hoping to leverage Socket.IO's built-in recovery mechanisms by delaying cleanup for unintentional disconnects.
🔮 Lastly How can the reconnection scenario be automatically tested?
I am using jest for testing. Manually I was simply using Chrome dev tool and setting the throttling to
offline
Thank you!
Beta Was this translation helpful? Give feedback.
All reactions