Infusion.Runtime.SSE (infusion_runtime v0.1.0)

Copy Markdown View Source

An incremental parser for Server-Sent Events (text/event-stream), as the HTML standard describes them.

Network chunks don't line up with events, so the parser keeps what it hasn't finished:

iex> alias Infusion.Runtime.SSE
iex> {[], state} = SSE.parse(SSE.new(), "event: delta\nda")
iex> {[event], _state} = SSE.parse(state, "ta: {\"x\": 1}\n\n")
iex> {event.event, event.data}
{"delta", "{\"x\": 1}"}

Lines end with \n, \r\n or \r, even when a \r\n is split across two chunks. data lines of one event are joined with \n. Comments (: keep-alive) are skipped.

Summary

Functions

Ends the stream: returns an event that was still open (the standard discards it; servers that forget the last blank line are common enough to be forgiving).

A parser with nothing buffered.

Feeds one chunk. Returns the events it completed and the new state.

Types

t()

@type t() :: %Infusion.Runtime.SSE{
  buffer: term(),
  data: term(),
  event: term(),
  id: term(),
  retry: term(),
  skip_lf?: term()
}

Functions

flush(state)

@spec flush(t()) :: [Infusion.Runtime.SSE.Event.t()]

Ends the stream: returns an event that was still open (the standard discards it; servers that forget the last blank line are common enough to be forgiving).

new()

@spec new() :: t()

A parser with nothing buffered.

parse(state, chunk)

@spec parse(t(), binary()) :: {[Infusion.Runtime.SSE.Event.t()], t()}

Feeds one chunk. Returns the events it completed and the new state.