Urchin.SSE (Urchin v0.2.0)

Copy Markdown View Source

Server-Sent Events framing helpers for the Streamable HTTP transport.

Event IDs are namespaced per stream (<stream_id>:<seq>) so the server can map a Last-Event-ID back to the originating stream during resumption, as the transport spec requires.

Summary

Functions

Builds an SSE comment line, useful as a keep-alive heartbeat.

Builds a per-stream event id of the form <stream_id>:<seq>.

Formats a JSON-RPC message as an SSE event with the given id.

Parses a Last-Event-ID value into {stream_id, seq}.

Builds the priming event: an event id with an empty data field, sent first so the client can immediately reconnect using that id as Last-Event-ID.

Builds a retry directive instructing the client how long to wait before reconnecting.

Functions

comment(text)

@spec comment(String.t()) :: iodata()

Builds an SSE comment line, useful as a keep-alive heartbeat.

event_id(stream_id, seq)

@spec event_id(String.t(), non_neg_integer()) :: String.t()

Builds a per-stream event id of the form <stream_id>:<seq>.

message(id, json)

@spec message(String.t(), iodata()) :: iodata()

Formats a JSON-RPC message as an SSE event with the given id.

The JSON payload is emitted on a single data: line (JSON never contains raw newlines once encoded).

parse_event_id(value)

@spec parse_event_id(String.t()) :: {String.t(), non_neg_integer()} | :error

Parses a Last-Event-ID value into {stream_id, seq}.

Returns :error when the value is not a recognised per-stream id.

priming(id)

@spec priming(String.t()) :: iodata()

Builds the priming event: an event id with an empty data field, sent first so the client can immediately reconnect using that id as Last-Event-ID.

retry(ms)

@spec retry(non_neg_integer()) :: iodata()

Builds a retry directive instructing the client how long to wait before reconnecting.