diff --git a/api/sse_events.php b/api/sse_events.php index 59d3cfa..3a703e1 100644 --- a/api/sse_events.php +++ b/api/sse_events.php @@ -79,16 +79,18 @@ try { error_log('SSE: Conexión en modo global (sin autenticación específica)'); } - $db = Database::getInstance(); - - // Enviar evento inicial de conexión - error_log("SSE: Nueva conexión establecida (userId: {$userId})"); + // IMPORTANTE: Enviar evento connected ANTES de hacer cualquier otra cosa + error_log("SSE: Enviando evento connected (userId: {$userId})"); sendSSEEvent('connected', [ 'timestamp' => time(), 'user_id' => $userId, - 'mode' => $authenticated ? 'authenticated' : 'global' + 'mode' => $authenticated ? 'authenticated' : 'global', + 'status' => 'ready' ]); + // Ahora sí, conectar a BD + $db = Database::getInstance(); + // Obtener último timestamp conocido por el cliente $lastEventId = $_SERVER['HTTP_LAST_EVENT_ID'] ?? null; $lastCheck = time(); diff --git a/api/sse_minimal.php b/api/sse_minimal.php new file mode 100644 index 0000000..ce0b892 --- /dev/null +++ b/api/sse_minimal.php @@ -0,0 +1,8 @@ + { + console.debug('Carga inicial de notificaciones omitida (SSE activo):', e.message); + }); // Backup: verificar cada 60 segundos (solo por si SSE falla) - setInterval(() => this.loadNotifications(), 60000); + setInterval(() => { + this.loadNotifications().catch(e => { + console.debug('Polling de notificaciones omitido (SSE activo)'); + }); + }, 60000); } /** @@ -1294,12 +1300,13 @@ const resp = await fetch('api/get_notifications.php', { credentials: 'same-origin' }); if (!resp.ok) { - console.warn(`Notifications fetch: HTTP ${resp.status} ${resp.statusText}`); + // Si falla, no es problema: SSE manejará las notificaciones en tiempo real + console.debug(`Notifications fetch: HTTP ${resp.status} (SSE manejará las notificaciones)`); return; } if (resp.status === 401) { - console.warn('Notifications fetch: unauthorized (session may have expired)'); + console.debug('Notifications fetch: unauthorized (SSE manejará las notificaciones)'); return; } @@ -1308,10 +1315,11 @@ if (json && json.success && Array.isArray(json.data)) { json.data.forEach(n => this.showNotificationToast(n)); } else if (json && json.success === false) { - console.warn('get_notifications returned error:', json.error || json); + console.debug('get_notifications returned error (no crítico, SSE activo):', json.error || json); } } catch (e) { - console.error('Error loading notifications', e); + // Error no crítico: SSE manejará las notificaciones en tiempo real + console.debug('loadNotifications failed (SSE manejará las notificaciones):', e.message); } }