I have noticed that I often have to refresh the page to see the latest messages from
Humans or Claude. How can i get realtime updates?
I have noticed that I often have to refresh the page to see the latest…
Short answer: realtime already exists, it just isn't resilient. Every post insert and status change fires pg_notify from a DB trigger (drizzle/0001_notify.sql), lib/events.ts LISTENs, /api/threads/[id]/events streams it as SSE, and components/thread-live.tsx calls router.refresh() when a post/status event lands. When it works you see the green "live" dot. The ways it goes stale, from reading the code:
- Reconnects never catch up. Vercel cuts the stream at the function's max duration (the events route sets none; the posts routes set 300s). The browser's
EventSourcereconnects silently, but anything posted in the gap is lost and the client ignores the secondhello. - Half-dead connections look alive. The keepalive is an SSE comment (
: ping), which JS cannot see, so after a wifi→cellular switch or a locked phone the dot stays "live" while nothing arrives. The "polling" fallback only kicks in after 3 connection errors, which a zombie socket never produces. - Backgrounded tabs. iOS Safari suspends the stream when you lock the phone / switch apps; nothing refreshes on return.
- LISTEN over a pooled URL.
lib/events.tsusesDATABASE_URL_UNPOOLED ?? DATABASE_URL. Through Neon's pgbouncer (-poolerhost) LISTEN connects fine but never delivers a notification, so the dot says "live" forever with no events. IfDATABASE_URL_UNPOOLEDisn't set on Vercel this alone explains everything; the README doesn't mention it. I can't read prod env from the sandbox, so please check. - A failed LISTEN poisons
__threadListenReadyfor the life of the instance, andhellois sent beforesubscribe()resolves (small gap).
Plan (all in threadide):
components/thread-live.tsx: refresh on every reconnect (helloafter the first), onvisibilitychange→visible /pageshow/online/ window focus, watchdog that reopens the stream + refreshes when no server event arrives for ~60s, coalesce bursts of events into onerouter.refresh(), and a slow safety poll while the thread isrunning.app/api/threads/[id]/events/route.ts:maxDuration = 300, keepalive as a namedpingevent (so the client can see it), subscribe beforehello, and if LISTEN can't be established send afallbackevent so the client polls instead of pretending to be live.lib/events.ts: don't cache a rejected LISTEN promise; warn once at startup when the LISTEN URL looks pooled (-pooler) and no unpooled URL is set.components/agent-activity.tsx: reuse the same stream-with-watchdog logic rather than a second bareEventSource.- README +
.env.example: documentDATABASE_URL_UNPOOLED.
No migration, no schema change. I'll verify against a local Postgres with the trigger applied (notify → listen → SSE) and type-check.
threadide@4ccf20eAnswer to the question: you already have realtime, it was just unreliable. Every post insert and status change fires a Postgres trigger (drizzle/0001_notify.sql) → LISTEN in lib/events.ts → SSE at /api/threads/[id]/events → ThreadLive refreshes the page. The green "live" dot next to the status badge is that stream. The problem was that the page went stale without the dot ever admitting it, in four ways, all fixed on thread/ac2e7dc5 (4ccf20e):
| Why it went stale | Fix |
|---|---|
| Vercel cuts the stream at the function's max duration (the route set none); the browser reconnects silently but nothing posted in the gap was refetched | maxDuration = 300; the client refetches on every reconnect |
| Keepalive was an SSE comment, invisible to JS, so a half-dead socket (phone switched wifi→cellular) stayed "live" forever | named ping every 20s + a 65s client watchdog that reopens the stream |
| A backgrounded / locked phone tab never caught up | stream closes while hidden, reopens + refetches on visible, pageshow, online, focus |
LISTEN through a transaction pooler (Neon's -pooler URL) connects but never delivers; looked identical to "live" | server self-probes a NOTIFY after LISTEN; if it never arrives the stream says fallback and the dot turns yellow "polling" (8s), with [events] LISTEN unavailable in the logs |
Plus: one shared EventSource per thread (components/use-thread-stream.ts) instead of two per viewer, coalesced refreshes, and a slow safety poll underneath (20s while a run is on, 60s idle) so a lost notification is at worst a minute late.
One thing to check on Vercel, @osebo: if DATABASE_URL there is Neon's pooled URL and DATABASE_URL_UNPOOLED is not set, point 4 is probably the main cause of what you saw. After this deploys you'll see a yellow "polling" dot on thread pages in that case; set DATABASE_URL_UNPOOLED (the Neon integration provides it) and it goes green. Documented in README + .env.example.
Verified with a local Postgres + the repo's migrations/seed and next dev: trigger → LISTEN → SSE events arrive (tsx + curl); Playwright shows a psql-inserted post on the page in ~0.9s without a reload, a post made while the tab is hidden appears ~0.8s after it is shown, and with the listener pointed at the wrong database the dot reads "polling" and the post still lands after one 8s poll (screenshot). tsc clean. Not reproducible here: the real Vercel duration cut and real pgbouncer; both are simulated by the equivalent paths (stream close/reopen, listener on a different DB).
Out of scope: live updates on the thread list pages; they still refresh on navigation.
Merged threadide #162 by @osebo (merge, 7e68988).
