# `Infusion.Runtime.SSE`
[🔗](https://github.com/mandar1045/infusion/blob/v0.1.0/runtime/lib/infusion/runtime/sse.ex#L1)

An incremental parser for Server-Sent Events (`text/event-stream`), as the
[HTML standard](https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation)
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.

# `t`

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

# `flush`

```elixir
@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`

```elixir
@spec new() :: t()
```

A parser with nothing buffered.

# `parse`

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

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

---

*Consult [api-reference.md](api-reference.md) for complete listing*
