Enhance SSE support: adjust headers and message format for better compatibility
Despliegue Automático / desplegar (push) Successful in 40s

This commit is contained in:
2026-09-09 14:20:27 +02:00
parent cdaecafcbb
commit e7da512a31
2 changed files with 15 additions and 4 deletions
+6 -1
View File
@@ -48,6 +48,7 @@ const streamEvents = async (req, res) => {
res.setHeader('Content-Type', 'text/event-stream'); res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Cache-Control', 'no-cache'); res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Connection', 'keep-alive'); res.setHeader('Connection', 'keep-alive');
res.setHeader('X-Accel-Buffering', 'no');
res.flushHeaders(); // Enviar cabeceras inmediatamente res.flushHeaders(); // Enviar cabeceras inmediatamente
console.log(`Cliente SSE conectado para userId: ${userId}`); console.log(`Cliente SSE conectado para userId: ${userId}`);
@@ -56,7 +57,11 @@ const streamEvents = async (req, res) => {
sseManager.addClient(userId, res); sseManager.addClient(userId, res);
// Latido (Heartbeat) para mantener la conexión viva a través de firewalls // Latido (Heartbeat) para mantener la conexión viva a través de firewalls
const interval = setInterval(() => res.write(':keepalive\n\n'), 30000); const interval = setInterval(() => {
res.write('event: ping\n');
res.write('data: keepalive\n\n');
if (typeof res.flush === 'function') res.flush();
}, 30000);
// Cuando el cliente (móvil/PC) cierre la app, limpiar la memoria // Cuando el cliente (móvil/PC) cierre la app, limpiar la memoria
req.on('close', () => { req.on('close', () => {
+9 -3
View File
@@ -24,13 +24,19 @@ class SseManager {
} }
} }
// El "timbre": Avisar a todos los dispositivos de un usuario (excepto al que originó el cambio)
notifyUser(userId) { notifyUser(userId) {
if (this.clients.has(userId)) { if (this.clients.has(userId)) {
const userClients = this.clients.get(userId); const userClients = this.clients.get(userId);
userClients.forEach(res => { userClients.forEach(res => {
// Formato obligatorio de SSE: "data: {JSON}\n\n"
res.write(`data: ${JSON.stringify({ event: 'sync_needed' })}\n\n`); // 1. Formato estricto que espera el paquete flutter_client_sse
res.write('event: message\n');
res.write('data: sync_needed\n\n');
// 2. Si hay algún middleware de compresión atrapando el mensaje, lo forzamos a salir
if (typeof res.flush === 'function') {
res.flush();
}
}); });
} }
} }