SSE and Durable Streams
Clients that only listen don't need a WebSocket. Cabby serves the same streams over two HTTP endpoints, both off by default:
| Turn it on | Path | For | |
|---|---|---|---|
| Server-Sent Events | --sse |
/events (--sse_path) |
the browser's EventSource, live updates |
| Durable Streams | --ds |
/ds (--ds_path) |
reading a stream from an offset, with explicit cursors |
Both read stream history from the broker, so for resuming, pick one in
History, recovery and presence. Both keep at most
--ws_max_pending_size bytes (1 MiB) waiting for a client, as a WebSocket does: a client that
falls further behind is disconnected, and counted in disconnected_slow_clients_total.
Server-Sent Events
The startup log has Handle SSE requests at http://localhost:8080/events. A request names one
subscription in its query:
| Parameter | Subscribes to |
|---|---|
signed_stream |
a signed stream (Authentication) |
stream |
a public stream, with --public_streams |
turbo_signed_stream_name |
a Turbo stream, as turbo_stream_from signs it |
channel |
one of your channels, through RPC, for example ?channel=ChatChannel |
identifier |
a full channel identifier, as JSON |
Two more change what comes back: raw=true and history_since. A request without a subscription
gets 400. signed_stream and stream subscribe through $pubsub, or through the channel named
in pubsub_channel under [streams] in the configuration file; so do Durable Streams reads.
The request is authenticated as a WebSocket connection is: your app's connect gets its cookies,
a JWT can come in ?jid=, and --noauth skips the check. A rejected request gets 401.
const events =
events.
Each broadcast is a message event whose data is what your app sent. Protocol messages come as
named events:
event: welcome
data: {"type":"welcome","sid":"Weks5qDcgJc2mUfFYqq-f"}
event: confirm_subscription
data: {"type":"confirm_subscription","identifier":"{\"channel\":\"$pubsub\",\"signed_stream_name\":\"InJvb206MSI=--71d8e5820afb4f0f7cc590cf34181efd9cd812345dae2dd46d04d4b5362a73b3\"}"}
data: {"n":1}
id: 1/iFWm/room:1
event: pingarrives every--ping_intervalseconds (3).--sse_comment_pingssends pings as: pingcomments instead, whichEventSourceignores.raw=trueleaves out the protocol events, so only your data reachesonmessage; pings become: pingcomments.- CORS: responses carry
Access-Control-Allow-Originfor the origins in--allowed_origins, or for every origin while it's empty.
Resume with Last-Event-ID
With a broker, every message has an id: line, <offset>/<epoch>/<stream>. When the connection
drops, EventSource reconnects by itself and sends the last id it saw in Last-Event-ID. Cabby
replays what came after it, then event: confirm_history, then the live messages:
A fresh EventSource, after a page reload for example, has no id to send. Give it
history_since, in Unix seconds, to replay from that time: the FastAPI guide does
this for AI replies. A Last-Event-ID takes precedence over history_since.
Turbo Streams over SSE
With --turbo_streams, Cabby checks Turbo's signed names itself (Hotwire), and a page
can listen without a WebSocket. The data of each event is the <turbo-stream> element your app
broadcast:
data: <turbo-stream action="append"></turbo-stream>
Durable Streams
Durable Streams read a stream over HTTP from an offset the client keeps. The stream's name is the
path, and the signed name goes in ?signed= or the X-Signed header (or no signature, with
--public_streams). Cabby serves reads only; your broadcasts write the stream.
--ds_skip_auth skips connection authentication, so only the signed name is checked. Without it,
a request is authenticated like a WebSocket connection: a rejected one gets 401, and one whose
RPC call fails gets 500. Read everything so far:
HTTP/1.1 200 OK
Content-Type: application/json
Stream-Next-Offset: 3::va6u
Stream-Up-To-Date: true
[{"n":1},{"n":2},{"n":3}]
Keep Stream-Next-Offset and pass it as offset next time.
offset |
Reads from |
|---|---|
-1 |
the start of the history |
now |
the end: only what comes next |
a Stream-Next-Offset value |
right after what you already have |
live |
Then |
|---|---|
| none | the history up to now, at once |
long-poll |
waits for the next message; 204 after --ds_poll_interval seconds (10) without one |
sse |
an SSE response: data events with arrays of messages, each followed by a control event with streamNextOffset, for up to --ds_sse_ttl seconds (60) |
HEAD returns only the headers, with the current end in Stream-Next-Offset. Durable Streams need
a broker: without one, a read gets 500 and Cabby logs failed to get stream metadata with
err="history not supported".
Which one
| WebSocket | SSE | Durable Streams | |
|---|---|---|---|
| Client sends messages | Yes | No | No |
| Browser client | @cabby/web, @rails/actioncable, Turbo |
EventSource |
fetch or EventSource |
| Resumes after a drop | session and history | Last-Event-ID |
the offset it keeps |
| Presence and whispers | Yes | No | No |