sse_parser v0.1.0 SseParser

Server sent event parser acording to w3c using NimbleParsec ref: https://www.w3.org/TR/2009/WD-eventsource-20090421

ABNF: stream = [ bom ] event event = ( comment / field ) end-of-line comment = colon any-char end-of-line field = 1name-char [ colon [ space ] *any-char ] end-of-line end-of-line = ( cr lf / cr / lf / eof ) eof = < matches repeatedly at the end of the stream >

; characters lf = %x000A ; U+000A LINE FEED cr = %x000D ; U+000D CARRIAGE RETURN space = %x0020 ; U+0020 SPACE colon = %x003A ; U+003A COLON bom = %xFEFF ; U+FEFF BYTE ORDER MARK name-char = %x0000-0009 / %x000B-000C / %x000E-0039 / %x003B-10FFFF

              ; a Unicode character other than U+000A LINE FEED, U+000D CARRIAGE RETURN, or U+003A COLON

any-char = %x0000-0009 / %x000B-000C / %x000E-10FFFF

              ; a Unicode character other than U+000D CARRIAGE RETURN or U+003A COLON

Link to this section Summary

Functions

Parse string to sse events, returning parsed events and unparsed part of input, unparsed part can be used when next chunk from sse arrive

Link to this section Types

Specs

comment() :: {:comment, String.t()}

Specs

error() ::
  {:error, String.t(), String.t(), map(), {integer(), integer()}, integer()}

Specs

event() :: [field() | comment()]
Link to this type

feed_error()

Specs

feed_error() ::
  {:error, String.t(), String.t(), map(), {integer(), integer()}, integer()}
Link to this type

feed_success()

Specs

feed_success() :: {:ok, [event()], String.t()}

Specs

field() ::
  {:field, [name: String.t(), data: String.t()] | [{:name, String.t()}]}

Link to this section Functions

Specs

feed(String.t()) :: feed_success() | feed_error()

Parse string to sse events, returning parsed events and unparsed part of input, unparsed part can be used when next chunk from sse arrive

Examples

iex> SseParser.feed(":Order 3 submitted\nevent: order-submitted\nreference: order 3\n\n")

iex> SseParser.feed(":Test event")

iex> {:ok, [], rest} = SseParser.feed(":Test event") iex> {:ok, [], rest} = SseParser.feed(rest <> "\nname: test") iex> SseParser.feed(rest <> "\n\n")