I have noticed that I often have to refresh the page to see the latest…

merged…
osebo29d ago

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?

3 repliesoldestnewest
Osebotfable-5-129d ago

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:

  1. 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 EventSource reconnects silently, but anything posted in the gap is lost and the client ignores the second hello.
  2. 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.
  3. Backgrounded tabs. iOS Safari suspends the stream when you lock the phone / switch apps; nothing refreshes on return.
  4. LISTEN over a pooled URL. lib/events.ts uses DATABASE_URL_UNPOOLED ?? DATABASE_URL. Through Neon's pgbouncer (-pooler host) LISTEN connects fine but never delivers a notification, so the dot says "live" forever with no events. If DATABASE_URL_UNPOOLED isn'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.
  5. A failed LISTEN poisons __threadListenReady for the life of the instance, and hello is sent before subscribe() resolves (small gap).

Plan (all in threadide):

  • components/thread-live.tsx: refresh on every reconnect (hello after the first), on visibilitychange→visible / pageshow / online / window focus, watchdog that reopens the stream + refreshes when no server event arrives for ~60s, coalesce bursts of events into one router.refresh(), and a slow safety poll while the thread is running.
  • app/api/threads/[id]/events/route.ts: maxDuration = 300, keepalive as a named ping event (so the client can see it), subscribe before hello, and if LISTEN can't be established send a fallback event 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 bare EventSource.
  • README + .env.example: document DATABASE_URL_UNPOOLED.

No migration, no schema change. I'll verify against a local Postgres with the trigger applied (notify → listen → SSE) and type-check.

Osebotfable-5-129d agothreadide@4ccf20e

Answer 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 staleFix
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 refetchedmaxDuration = 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" forevernamed ping every 20s + a 65s client watchdog that reopens the stream
A backgrounded / locked phone tab never caught upstream 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.

Fallback path in the browser: listener cannot receive NOTIFYs, so the dot reads "polling" (yellow) instead of a false "live"; posts still arrived within 8s
Fallback path in the browser: listener cannot receive NOTIFYs, so the dot reads "polling" (yellow) instead of a false "live"; posts still arrived within 8s
Osebot29d ago

Merged threadide #162 by @osebo (merge, 7e68988).