fix(socket): improve connection stability with heartbeat and auto-reconnect

- Backend: Add pingInterval (25s) and pingTimeout (60s) for better keep-alive
- Frontend: Increase reconnection attempts to Infinity (keep trying forever)
- Frontend: Add reconnect event handlers to rejoin rooms after reconnection
- Frontend: Check initial connection state when reusing socket instance
This commit is contained in:
Radosław Gierwiało
2025-11-21 21:53:51 +01:00
parent 78280ca8d8
commit 8a369c1fc4
4 changed files with 60 additions and 9 deletions

View File

@@ -64,8 +64,18 @@ const useMatchChat = (match, userId, slug) => {
// Socket event listeners
socket.on('connect', joinMatchRoom);
socket.on('disconnect', () => {
socket.on('disconnect', (reason) => {
setIsConnected(false);
console.log('🔌 Match chat disconnected:', reason);
});
// Handle reconnection
socket.on('reconnect', (attemptNumber) => {
console.log('🔄 Match chat reconnected after', attemptNumber, 'attempts');
});
socket.on('reconnect_attempt', (attemptNumber) => {
console.log('🔄 Match chat reconnection attempt', attemptNumber);
});
// Receive messages
@@ -82,6 +92,8 @@ const useMatchChat = (match, userId, slug) => {
return () => {
socket.off('connect', joinMatchRoom);
socket.off('disconnect');
socket.off('reconnect');
socket.off('reconnect_attempt');
socket.off('match_message');
};
}, [match, userId]);