Real-time updates with SSE
Subscribe, reconnect, and reconcile organization updates.
Real-time updates with SSE
Server-Sent Events (SSE) keeps an HTTP connection open to signal changes. Notifications tell the UI when to refresh; the resource returned by its regular endpoint remains authoritative.
Available streams
| Stream | Access | Use |
|---|---|---|
.../sse/events/by-slug/{eventSlug} | Public | Visible changes to a concrete Event |
.../sse/campaigns/{campaignId} | manager | Campaign progress and results |
.../sse/events/{eventId}/attendees | manager | Registrations, confirmations, and check-in |
.../sse/invoices | manager | Invoice changes and reconciliation |
.../sse/whatsapp | manager | WhatsApp messages and sessions |
All use /api/organizations/{organizationSlug} as their prefix.
Connect from the browser
EventSource sends cookies on a same-origin URL:
const stream = new EventSource(
"/api/organizations/acme/sse/invoices",
)
stream.onmessage = async (event) => {
const signal = JSON.parse(event.data)
await refreshInvoices(signal)
}
stream.onerror = () => {
// EventSource attempts to reconnect automatically.
}Do not add the session cookie or token to a query string. For different hosts, design a secure backend proxy rather than exposing credentials in the browser.
Correct processing
- Parse the event as a small signal.
- Deduplicate by ID when available.
- Fetch the affected resource or page again.
- Replace local state with the authoritative response.
- Preserve current filters and pagination.
Events can be grouped, repeated, or arrive after a reconnection. Do not increment counters or apply payments using only the SSE payload.
Reconnection
The browser retries automatically. The interface should show “updating” if the connection drops, keep the last data readable, and perform a complete read after reconnecting. If the server sends an event ID, retain Last-Event-ID according to EventSource behavior.
Avoid creating multiple streams for the same view. Call stream.close() on unmount, organization switch, or logout.
Security
- Protected streams recheck session and membership.
- Switching tenants requires closing the old connection and opening a new URL.
- An impersonation session is read-only; do not use an SSE signal to enable write controls.
- Do not include message bodies, tokens, or payment data in frontend logs.
Diagnosis
If no events arrive, verify URL, organization, role, cookie, proxies that buffer responses, and a text/event-stream server response. Then trigger a controlled change and confirm that a REST read reflects it even if the stream fails.