seguimos intentado hacer funcionar el realtime
All checks were successful
build-and-deploy / filter (push) Successful in 2s
build-and-deploy / build (push) Successful in 7s
build-and-deploy / deploy (push) Successful in 32s

This commit is contained in:
2025-06-09 18:01:46 -06:00
parent e803d3b16a
commit caf3e886a7

View File

@@ -7,23 +7,30 @@ export function registerSse(app) {
res.set({ res.set({
'Content-Type': 'text/event-stream', 'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache', 'Cache-Control': 'no-cache',
'Connection': 'keep-alive' Connection: 'keep-alive'
}); });
res.flushHeaders(); res.flushHeaders();
res.write('event: connected\ndata: {}\n\n'); // ← mantiene el stream vivo
// Primer “hola”
res.write('event: connected\ndata: {}\n\n');
// 🔴 Mantén viva la conexión con pings cada 15 s
const keepAlive = setInterval(() => {
res.write(':\n\n'); // comentario SSE = ping
}, 15000);
sseClients.push(res); sseClients.push(res);
console.log(`🟢 Cliente SSE conectado (${sseClients.length})`); console.log('🟢 Cliente SSE conectado (%d)', sseClients.length);
req.on('close', () => { req.on('close', () => {
const idx = sseClients.indexOf(res); clearInterval(keepAlive); // limpia el intervalo
if (idx !== -1) sseClients.splice(idx, 1); sseClients.splice(sseClients.indexOf(res), 1);
console.log(`🔌 Cliente SSE desconectado (${sseClients.length})`); console.log('🔌 Cliente SSE desconectado (%d)', sseClients.length);
}); });
}); });
const broadcast = (data) => { const broadcast = (data) => {
const payload = `data: ${data}\n\n`; const payload = `data: ${data}\n\n`;
sseClients.forEach((client) => client.write(payload)); sseClients.forEach((client) => client.write(payload));