From b56f1fdf7e10c2fc9aeebc9afc4587e2c8a3daa8 Mon Sep 17 00:00:00 2001 From: Chris Scott <99081550+chriswritescode-dev@users.noreply.github.com> Date: Tue, 27 Jan 2026 09:39:58 -0500 Subject: [PATCH] Add SSE heartbeat to prevent Cloudflare 524 timeout --- backend/src/routes/sse.ts | 12 ++++++++++++ frontend/src/lib/sseManager.ts | 4 ++++ shared/src/config/defaults.ts | 1 + 3 files changed, 17 insertions(+) diff --git a/backend/src/routes/sse.ts b/backend/src/routes/sse.ts index 14d36474..980e2caf 100644 --- a/backend/src/routes/sse.ts +++ b/backend/src/routes/sse.ts @@ -3,6 +3,9 @@ import { stream } from 'hono/streaming' import { sseAggregator } from '../services/sse-aggregator' import { SSESubscribeSchema } from '@opencode-manager/shared/schemas' import { logger } from '../utils/logger' +import { DEFAULTS } from '@opencode-manager/shared/config' + +const { HEARTBEAT_INTERVAL_MS } = DEFAULTS.SSE export function createSSERoutes() { const app = new Hono() @@ -36,7 +39,16 @@ export function createSSERoutes() { directories ) + const heartbeatInterval = setInterval(() => { + try { + writeSSE('heartbeat', JSON.stringify({ timestamp: Date.now() })) + } catch { + clearInterval(heartbeatInterval) + } + }, HEARTBEAT_INTERVAL_MS) + writer.onAbort(() => { + clearInterval(heartbeatInterval) cleanup() }) diff --git a/frontend/src/lib/sseManager.ts b/frontend/src/lib/sseManager.ts index 87cdd0b3..20dc48b7 100644 --- a/frontend/src/lib/sseManager.ts +++ b/frontend/src/lib/sseManager.ts @@ -207,6 +207,10 @@ class SSEManager { // Ignore } }) + + this.eventSource.addEventListener('heartbeat', () => { + // Heartbeat received - connection is alive + }) } private disconnect(): void { diff --git a/shared/src/config/defaults.ts b/shared/src/config/defaults.ts index 12a0a813..25451054 100644 --- a/shared/src/config/defaults.ts +++ b/shared/src/config/defaults.ts @@ -47,6 +47,7 @@ export const DEFAULTS = { RECONNECT_DELAY_MS: 1000, MAX_RECONNECT_DELAY_MS: 30000, IDLE_GRACE_PERIOD_MS: 5000, + HEARTBEAT_INTERVAL_MS: 60000, }, } as const