Improve session reconnect behavior

This commit is contained in:
Federico Jaramillo Martinez
2026-05-07 13:00:10 +02:00
parent fd28af3c93
commit ad05497319
10 changed files with 114 additions and 15 deletions
+13
View File
@@ -2,6 +2,7 @@ import type { WebSocket } from "ws";
export class SessionEventHub {
private readonly socketsBySession = new Map<string, Set<WebSocket>>();
private readonly globalSockets = new Set<WebSocket>();
add(sessionId: string, socket: WebSocket): void {
let sockets = this.socketsBySession.get(sessionId);
@@ -13,10 +14,22 @@ export class SessionEventHub {
socket.on("close", () => sockets?.delete(socket));
}
addGlobal(socket: WebSocket): void {
this.globalSockets.add(socket);
socket.on("close", () => this.globalSockets.delete(socket));
}
publish(sessionId: string, event: unknown): void {
const payload = JSON.stringify(event);
for (const socket of this.socketsBySession.get(sessionId) ?? []) {
if (socket.readyState === socket.OPEN) socket.send(payload);
}
}
publishGlobal(event: unknown): void {
const payload = JSON.stringify(event);
for (const socket of this.globalSockets) {
if (socket.readyState === socket.OPEN) socket.send(payload);
}
}
}