Skip to content

Commit a1da41b

Browse files
committed
fix: add ping to SSE routes to keep connection alive
1 parent 99fc59b commit a1da41b

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

src/routes/sse.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,16 @@ export const setupServerSideEventsRoutes = function(app: Express, prisma: Prisma
7979
}
8080
});
8181
};
82+
83+
// Ping all clients every 15 seconds to keep the connection alive
84+
setInterval(() => {
85+
for (const stream in sseClients) {
86+
sseClients[stream as SSEStream].forEach(client => {
87+
try {
88+
triggerSSE(stream as SSEStream, 'ping', { timestamp: Date.now() });
89+
} catch (err) {
90+
console.error('Error writing ping to SSE client:', err);
91+
}
92+
});
93+
}
94+
}, 15000);

static/js/live-scores.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ scoresEventSource.onopen = function() {
77
console.log('Scores SSE connection opened.');
88
};
99

10+
scoresEventSource.addEventListener('ping', function(event) {
11+
console.log('Received ping from scores SSE.');
12+
});
13+
1014
scoresEventSource.addEventListener('new_score', function(event) {
1115
const data = JSON.parse(event.data);
1216
console.log('New score:', data);

0 commit comments

Comments
 (0)