@johntalton/sse-util
v0.0.4
Published
Set of Utilities and Defines for build HTTP for SSE
Downloads
4
Readme
Server Sent Events util
A simple set of transformation functions to abstract away the sse protocol.
API
Class ServerSentEvents
messageToEventStreamLines
retryToEventStreamLine
keepAliveToEventStreamLine
messageToEventStreamLines
Core method that converts a SSEMessage
into a set of Server Sent Event Lines.
const lines = ServerSentEvents.messageToEventStreamLines({ id: 42, event: '🎁', data: ['i got you this'] })
// result lines array:
// [ "event: 🎁\n", "data: i got you this\n", "id: 42\n", "\n" ]
retryToEventStreamLine
Alias to messageToEventStreamLines
that only returns the retryMs
value and extracts the first line as a single string
.
// create a line to request the retry interval be set to 5 sec
const line = ServerSentEvents.retryToEventStreamLine(5 * 1000)
// result "retry: 5000\n"
keepAliveToEventStreamLine
Alias to messageToEventStreamLines
that sets the comment and returns string
line instread of and Array
of lines.