Claudex.Stream.SSE (Claudex v0.6.1)

Copy Markdown View Source

Decodes a Server-Sent Events byte stream into events.

Incremental and network-free: feed it whatever bytes arrived, get back the events those bytes completed plus a decoder holding the leftover tail.

{events, decoder} = SSE.decode(SSE.new(), chunk)

Nothing is emitted until an event's terminating blank line arrives, so a chunk that splits an event mid-JSON yields nothing until the rest lands. Call flush/1 when the connection closes to drain a final event that came without its terminator.

Summary

Functions

Feeds chunk to the decoder, returning the events it completed and a decoder carrying whatever is still incomplete.

Drains the buffer, returning any event held in it and an empty decoder.

Builds a decoder with an empty buffer.

Types

t()

@type t() :: %Claudex.Stream.SSE{buffer: binary()}

Functions

decode(sse, chunk)

@spec decode(t(), binary()) :: {[Claudex.Stream.SSE.Event.t()], t()}

Feeds chunk to the decoder, returning the events it completed and a decoder carrying whatever is still incomplete.

Blocks holding only comments or unrecognised fields produce no event, so the returned list can be empty for a chunk that did contain data.

flush(sse)

@spec flush(t()) :: {[Claudex.Stream.SSE.Event.t()], t()}

Drains the buffer, returning any event held in it and an empty decoder.

A well-behaved server terminates every event with a blank line, so this is usually empty. It exists so an unterminated final event isn't silently dropped when the connection closes.

new()

@spec new() :: t()

Builds a decoder with an empty buffer.