Clean up session participants on socket disconnect
All checks were successful
Build & Push Container Image / build (push) Successful in 5s

When a user closes their browser or navigates away, the socket
disconnects without emitting poker:leave. The participant stayed
in the NATS session data indefinitely. Now the disconnect handler
iterates the socket's poker rooms and calls leaveSession for each,
then broadcasts the updated state to remaining participants.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Jan Willem Mannaerts 2026-03-03 13:42:51 +01:00
parent 5b83dd7ed0
commit a7aac985d2

View file

@ -194,7 +194,23 @@ io.on('connection', (socket) => {
websocketConnections.inc();
trackUniqueUser(user.jiraAccountId);
trackUniqueTenant(user.jiraCloudId);
socket.on('disconnect', () => { websocketConnections.dec(); });
socket.on('disconnect', async () => {
websocketConnections.dec();
for (const room of socket.rooms) {
if (!room.startsWith('poker:')) continue;
const sessionId = room.slice(6);
try {
await leaveSession({
sessionId,
tenantCloudId: socket.user.jiraCloudId,
userKey: socket.user.jiraAccountId
});
await emitSessionState(sessionId, socket.user.jiraCloudId);
} catch {
// best-effort cleanup
}
}
});
const throttled = socketThrottle(socket);
throttled('poker:join', async ({ sessionId }) => {