Frontend: @djangocfg/realtime
The browser half: one WebSocket per tab, opened lazily, reference-counted topics, replay after a reconnect, and a refetch when history no longer reaches the last offset. It implements protocol v1.
| Import | Holds |
|---|---|
@djangocfg/realtime | createRealtimeClient, watchTopics, the protocol types and close codes, the store, RealtimeTopic<E>. No React, no DOM |
@djangocfg/realtime/react | RealtimeProvider, RealtimeClientProvider, useTopic, useRealtimeState, useRealtimeClient |
@djangocfg/realtime/debug | RealtimePanel |
@djangocfg/realtime/testing | createMockServer() |
@djangocfg/realtime/styles | @source for the panel’s classes |
BaseApp mounts it
BaseApp from @djangocfg/layouts mounts the provider inside its QueryClient and AuthProvider. Do not mount a second RealtimeProvider. It opens nothing by itself: the first useTopic connects, and an app whose pages never subscribe never opens a socket.
<BaseApp project="shop" auth={{ apiUrl }} realtime={{ path: '/ws' }}>
{children}
</BaseApp>realtime | |
|---|---|
omitted / {} | On, with defaults |
url | The full socket URL; overrides the rest |
apiUrl | The API origin the URL is derived from |
path | Default /ws |
onGap | Your own refetch for a topic whose history no longer reaches the client’s offset |
onIssue | An extra sink for issues; they go to @djangocfg/devtools either way |
false | Off: no provider and no React Query bridge |
Without url, the socket URL is the API origin (realtime.apiUrl, else apiUrl, else auth.apiUrl, else NEXT_PUBLIC_API_URL, else the page’s own origin) plus path, with ws: for http: and wss: for https:.
useTopic
'use client';
import { useState } from 'react';
import { useTopic } from '@djangocfg/realtime/react';
import { demoPingTopic } from '@/api/generated/realtime';
export function LastPing() {
const [note, setNote] = useState<string | null>(null);
const state = useTopic(demoPingTopic(), (event) => setNote(event.note)); // `event` is typed DemoPingEventOutput
return <span>{note ?? '…'}{state?.status === 'live' ? '' : ' (connecting)'}</span>;
}When all you do on an event is refetch, you need no useTopic at all: mark the view @invalidated_by and let
BaseApp do it (next section).
useTopic<E>(topic: string | RealtimeTopic<E> | null, onEvent: (data: E, meta: EventMeta) => void,
options?: { onGap?: (topic) => void; onRefused?: (code, topic) => void }): TopicState | undefined- Typed topics. Pass a builder from the generated
realtime.ts:eventis typed by the topic’s event model, and in development every payload is checked against its zod schema; a mismatch is logged and not delivered. A plain string works too, untyped. nullsubscribes to nothing (a topic that depends on data not loaded yet).- Handlers may change identity on every render; only a new topic name resubscribes. Calling the builder inline is fine.
metais{ topic, offset, originCid }.originCidnames the connection whose request caused the publication.- The return value is the topic’s
TopicState:status(pending,live,refused,leaving),offset,subscribers,refusal,received,gaps. It isundefinedbefore the first subscription and outside a provider. onRefused(code)reports asub_err(forbidden,unknown_topic,invalid,limit,timeout). A refusal is not retried until the auth state changes, excepttimeout.
Zero-code refresh with React Query
A REST read whose view is marked @invalidated_by("orders:{order_id}") refreshes itself. The generated query
carries the concrete topic names in meta.realtimeTopics (see code generation), and
BaseApp watches its QueryCache:
- while at least one query with an observer (a mounted hook) carries a topic, that topic stays subscribed; when the last one goes, it is released (after the client’s 1.5 s delay, so a remount costs nothing);
- each message, each gap, and a fresh subscription going live invalidate the queries tagged with that topic, once
per burst of ~250 ms (
watchTopics).
Untagged queries cost nothing and open no socket. The page has no realtime code:
import { useRealtimeDemoPingsList } from '@/api/generated/endpoints/realtime-demo/realtime-demo';
export function PingList() {
const pings = useRealtimeDemoPingsList({ page_size: 10 }); // refetches when demo.pings publishes
return <div>{pings.data?.count ?? '…'} pings</div>;
}The revalidation on a fresh subscription catches a publication that landed between the fetch and the subscription.
A resubscribe after a reconnect replays with since instead.
This works only in BaseApp’s QueryClient. A client created at module scope, a second QueryClientProvider, or a
client passed to a hook as its last argument is a cache the bridge never sees. REALTIME_META_KEY and
topicsOfQuery(query) from @djangocfg/layouts read the tag, for an app that bridges another client itself.
Gaps
A gap means the server could not replay what the client missed: sub_ok with recovered: false after a reconnect with since, or a repeated sub_ok{recovered:false} on a live subscription (a new epoch, or the bus was trimmed past the server’s reader). The client adopts the new offset and calls onGap, jittered 0–2 s so a deploy does not refetch every page at once.
- With
onGapon theuseTopiccall: that runs. - Else the provider’s
onGap(theBaseApprealtime.onGap). - Else
BaseAppinvalidates every query tagged with the topic (meta.realtimeTopics).
A reconnect that replays everything is not a gap. A message that follows the gap carrying the offset it announced is delivered, not dropped as a duplicate. Never treat events as the only copy of the data: refetch on a gap.
Connection state
import { useRealtimeState } from '@djangocfg/realtime/react';
const connection = useRealtimeState(); // { phase, attempt, retryAt, user, cid, lastClose }
const topic = useRealtimeState((s) => s.topics['orders:42']); // one slicephase is idle, connecting, live, reconnecting or stopped. A selector must return a value already in the snapshot, never a new object or array: the snapshot shares structure, and a fresh object re-renders on every frame.
Behaviour
- Lazy. No socket until the first subscription; closed 30 s after the last one leaves. A topic is unsubscribed 1.5 s after its last subscriber, so a StrictMode remount or a route change costs nothing.
- Reconnect. Full-jitter backoff from the first retry (a random wait under 1 s, 2 s, 4 s … up to 30 s), and at once on
onlineor when the page becomes visible. The attempt counter resets only after 10 s live, so a server that accepts and drops at once still backs off. After 1011, 1012 or 1013 the first retry waits a random 0–5 s. 4429 retries after 30 s plus jitter. Topics resubscribe withsince= their last offset. - Keepalive. A
pingeveryhello.pingseconds; nopongwithin 10 s means the path is dead and the client reconnects. - Auth. The cookie goes with the handshake; the first frame is always
auth, with the token fromuseAuth().getToken()when there is one. Login or logout reconnects with the new identity. 4419 refreshes the token throughuseAuth().refreshToken()and reconnects at once (stopped when no session is left). Only 4401 and 4403 stop, until the auth state changes.
Issues
The client reports what a person has to fix, never normal weather. BaseApp sends each issue to @djangocfg/devtools (EventType.ERROR; level warning for 4401, unreachable and forbidden, error for the rest) and then to realtime.onIssue if you pass one. Outside BaseApp, createRealtimeClient({ onIssue }); without it the client calls console.warn.
kind | When |
|---|---|
closed | 4401, 4403, 4500, or a stop the client decided (code 0: no WebSocket, a bad URL, another protocol version) |
unreachable | 5 attempts in a row ended without hello; once per streak |
refused | sub_err forbidden, invalid or unknown_topic, with the topic |
protocol | an error frame, or a frame that could not be parsed |
invalid_event | a typed topic’s event failed its schema (development only) |
Every issue carries cid, the live connection’s id or null; none carries a token. Never reported: 1006, 1011, 1012, 1013 or 4408 drops, a 4419 refresh, sub_err limit or timeout.
No echo for your own writes
@djangocfg/api exports setRealtimeCidProvider(provider). Once a provider returns the socket’s id, every POST,
PUT, PATCH and DELETE sent through djangoFetch carries X-Realtime-Cid, and the server skips this tab’s socket
when the write publishes (details).
BaseApp installs it once. The provider lives in one slot per page (globalThis[Symbol.for('djangocfg.realtime.cid')]),
and every generated tree calls the same djangoFetch, so the install covers @djangocfg/api’s own operations and
your project’s make gen output alike. A request that does not go through djangoFetch adds the header itself.
client.getCid() is null while no socket is live; then no header is sent.
Debug panel
import { RealtimePanel } from '@djangocfg/realtime/debug';
<RealtimePanel />@import "@djangocfg/ui-core/styles/full";
@import "@djangocfg/realtime/styles"; /* after ui-core */It shows the connection (phase, user, connection id, attempt, next retry, last close, and a “Retry now” button while reconnecting), every topic with its status and offset, and the last 50 frames, with any auth token redacted. Frames are recorded only while a panel is mounted. It accepts isActive, so it fits a @djangocfg/devtools custom tab (CustomDebugTab.panel).
Outside React
import { createRealtimeClient } from '@djangocfg/realtime';
const client = createRealtimeClient({ url: 'wss://api.example.com/ws', getToken });
const release = client.subscribe('demo.ping', (data, meta) => console.log(meta.offset, data));
// later
release();subscribe also takes a typed topic and a handler object { onEvent, onGap?, onRefused? }.