nhttp_sse (nhttp v1.0.0)
View SourceServer-Sent Events (SSE) helpers for nhttp.
Provides convenience functions for building SSE responses per the W3C Server-Sent Events specification.
Example Usage
handle_request(#{path := <<"/events">>}, State) ->
Headers = nhttp_sse:headers(),
Producer = fun(SendChunk) ->
loop(SendChunk)
end,
Spec = nhttp_stream:producer(200, Headers, Producer),
{stream, Spec, State}.
loop(SendChunk) ->
Event = nhttp_sse:event(<<"update">>, <<"new data arrived">>),
case SendChunk(Event) of
ok -> loop(SendChunk);
{error, _} -> ok
end.
Summary
Functions
Encode a data field, properly handling multi-line data. Each line of data is prefixed with "data: " per the SSE spec.
Encode a data-only event (no event type). The data field may contain newlines. They will be properly encoded.
Encode a named event with data. The event type should not contain newlines.
Returns the required headers for an SSE response.
Returns SSE headers merged with additional headers.
Encode an event ID field. The id should not contain newlines.
Encode a retry field (reconnection time in milliseconds).
Functions
Encode a data field, properly handling multi-line data. Each line of data is prefixed with "data: " per the SSE spec.
Encode a data-only event (no event type). The data field may contain newlines. They will be properly encoded.
Encode a named event with data. The event type should not contain newlines.
Returns the required headers for an SSE response.
Returns SSE headers merged with additional headers.
Encode an event ID field. The id should not contain newlines.
-spec retry(non_neg_integer()) -> iolist().
Encode a retry field (reconnection time in milliseconds).