Use WebSockets
Mount a socket delegate on a path, authorize upgrades, and push typed events to connected clients.
WebSockets in Base are handled by delegates: a class per socket endpoint that authorizes connections and reacts to socket lifecycle. On Cloudflare, each connection is backed by a Durable Object, so sockets survive worker restarts (hibernation) without you managing any of it.
Write a delegate
Extend the abstract WebSocketDelegate. Three methods are required; two are optional:
The optional hooks: populateSocketContext(info, socketId) seeds per-socket context values on first use, and onWebSocketEvent(webSocket, info, event) receives non-RPC events sent over the socket.
Mount it
Delegates are registered in settings (worker-level webSocket, or contributed by a module):
Each delegate gets a GET route at its path. The connection flow: a standard WebSocket upgrade hits the path → no delegate is a 404, a non-upgrade request is a 426 → your authorizeUpgrade runs: undefined rejects with 401, a socket id accepts. Clients are ordinary platform WebSockets, browser or otherwise:
(There's no client library to install — the platform WebSocket is the client.)
Push to a connected socket
The delegate base class provides the send-side tools, keyed by socket id:
On Cloudflare each socket lives in its own Durable Object (keyed by the socket id); the delegate's helpers route through it transparently, worker-side or DO-side.
Session context on sockets
Modules can map request-context values onto the socket's context at connect time — this is how a socket knows which session opened it:
Read them back anywhere you hold the WebSocketInfo with getWsContext(info, key); write your own in populateSocketContext with setWsContext. The WebSocketInfo (socketId, path, context) follows the socket everywhere, including into RPC procedures invoked over it.