Source: src/utils/socketConnect.ts
Environment
Must match backend HTTP origin (same host/port as API server).
Singleton pattern
Only one socket instance per browser tab session.
connectSocket(userId)
Creates socket with query: { userId } — backend maps userId → socket.id in userSocketMap for targeted emits (updateUserSettingData).
On connect:
- Logs socket id
- Starts 15s
ping interval
disconnectSocket()
Called from JWTContext.logout():
- Clears heartbeat interval
removeAllListeners()
disconnect()
- Sets
socket = null
Reconnection
Socket.IO client configured with infinite reconnection attempts and 1s initial delay. Pages should tolerate brief disconnects and refresh on reconnect events if needed.
Usage example
Always socket.off the same handler reference in cleanup to avoid duplicate listeners on re-render.