Hopp til innholdet

DocumentationRealtimeMarkdown

Realtime

Snakkbar pushes small doorbell events; you fetch the actual content over HTTP. That keeps the stream cheap, makes missed events harmless (just refetch), and means one code path for initial load and updates.

Supabase Realtime (preferred)

Browsers listen to the doorbell straight from Supabase Realtime, so nothing runs on Snakkbar's side while a panel or chat waits. GET /api/v1/visitor/realtime and GET /api/v1/operator/realtime answer { realtime: { url, apikey, topics, token, expiresAt } } (or { realtime: null } when the deployment has no signing key): join each topic as a private channel on the socket with the token, and send a fresh one (access_token) before expiresAt; tokens last 15 minutes.

WhoTopicsHears
Visitorvisitor:<visitorId>, visitors:<tenant>Their conversations (conversation, message without notes or rating lines), staff typing/read, watch, cobrowse, cobrowse_pointer; the team's presence
Operatortenant:<tenant>Everything in the tenant, as the stream below

The token is signed by Snakkbar with an ES256 key imported into the Supabase project (Settings → JWT Keys, SNAKKBAR_REALTIME_JWK on the server). Its role is anon (no access to any table), and the policies on realtime.messages (supabase/migrations/20261103100000_realtime_direct.sql) let it join only the topics its snakkbar claim names. Broadcasts carry the raw doorbell (event and ids); a message never carries the message itself, so fetch it. The SDK's subscribe() does all of this, and falls back to the SSE streams below when Realtime cannot be reached (no token, a site's Content-Security-Policy refusing wss://<ref>.supabase.co, repeated failures).

Streams (fallback)

WhoEndpointReceives
VisitorGET /api/v1/visitor/stream?conversationId=<id>Events for the visitor's own conversations; typing/read for conversationId; presence; watch for this visitor
OperatorGET /api/v1/operator/streamEverything in the tenant, including live visitors (visitor, pointer). With ?scope=conversations without those two (the widget's operator mode uses this on every page)

Both are Server-Sent Events over a normal fetch with the Authorization header (the browser's EventSource can't send headers, so read response.body yourself or use the SDK).

event: ready
data: {"type":"ready"}

event: message
data: {"type":"message","conversationId":"…","messageId":4812}

event: typing
data: {"type":"typing","conversationId":"…","side":"operator","operator":{"id":"…","handle":"kari","name":"Kari","avatarUrl":null},"typing":true}

Events (RealtimeEvent)

typeFieldsDo
readyStream is live. Refetch once to cover anything missed while connecting.
pingEvery 20 s. Ignore.
conversationconversationIdStatus, owner, participants or read state changed → refetch it (and your list).
messageconversationId, messageIdNew message or a new translation → refetch the conversation.
typingconversationId, side, operator?, typingShow/hide a typing indicator. Hide it yourself after 6 s without updates.
readconversationId, side, atUpdate «Seen» receipts.
presenceSomeone checked in/out or came online → refetch config() (visitor) or team() (operator).
visitorvisitorIdOperator stream. A visitor came online, started a session, opened a page, switched tabs or changed their profile → refetch the visitor list (and that visitor's detail if shown).
pointerLiveFrame: visitorId, tabId, url, title, visible, viewport, scroll, documentHeight, samples, clicksOperator stream, only while someone watches that visitor. Draw it in the live view. The one event that carries data rather than a doorbell.
watchactiveVisitor stream. An operator started (true) or stopped (false) watching this visitor → switch the tracker's pace now. The SDK does this for you.
cobrowsevisitorId, session, reason?, by?Both streams. Co-browsing was requested, accepted or ended (cobrowse.md).
cobrowse.pointervisitorId, operatorId, url, samples, clicks, away?Visitor stream, only while the visitor is being guided: the operator's cursor in document coordinates.

Debounce refetches (the stock UIs use 250 ms) — one visitor message can ring several times (message, translation, conversation update).

Live visitors

The visitor's browser (the SDK's startTracking(), used by the stock widget) drives the live visitor feed with POST /visitor/activity:

Cadence
HeartbeatAs the response's interval says, from every open tab: every 2 min (5 min in the background) when the page listens on Supabase Realtime and says so (realtime: true), else every 20 s (2 min in the background). Plus at once on a page load, a single-page-app URL change, a tab switch, and (with Realtime) a staff message or a change of connection.
OnlineA heartbeat within the last 2.5 min, or 15 min for a tab in the background; 30 s after a tab said it left. Nothing rings when a visitor goes offline: re-check lastSeenAt on a timer.
SessionStarts with a heartbeat after 30 minutes of silence.
WatchPOST /operator/visitors/:id/watch lasts 25 s; renew it every 10 s (the SDK's visitors.watch() does). DELETE ends it at once.
While watchedEach tab sends a batch about every 500 ms (pointer positions sampled at most every 40 ms, up to 100 per batch, and up to 20 clicks), or a keep-alive every 2 s when nothing moved. Every batch becomes one pointer event.

The browser learns about a watch from the watch event on its Realtime topic (the SDK listens on every tracked page, sharing one connection with the chat), otherwise from the next heartbeat response ({ watch: true }), so without Realtime a live view can take up to 20 s to start. It falls back to heartbeats when a response says watch: false, e.g. 25 s after the last renewal.

Using a pointer event: samples and clicks are [t, x, y], where t is milliseconds since the batch's first sample and x/y are viewport (client) coordinates in CSS px; replay them over the batch for smooth motion. Scale them by your view's width divided by viewport.width, and scroll your copy of the page to scroll. Several tabs of one visitor can send frames; follow the one with visible: true. pointer events go to every operator's stream, so filter by visitorId.

Lifetime and fallback

The server ends each stream after about 4 minutes; reconnect immediately. On errors, back off (1 s, 2 s, 5 s). If streaming keeps failing (some proxies buffer SSE), poll the relevant GET every 5 s and try streaming again every 60 s. The SDK's subscribe() does all of this and, while polling, emits conversation events — with conversationId: "*" on the operator side, meaning "refetch everything". Polling can't carry pointer events: a live view only moves while the operator stream is live (onStatus reports "live").

Turn streams

Sending a visitor message (POST /visitor/conversations/:id/messages) returns its own SSE stream with the assistant's reply as it is written (TurnEvents, see api.md). Doorbell events for the same messages also arrive on the realtime stream; dedupe by message id (or clientId).