0.0.4 • Published 11 months ago

@johntalton/sse-util v0.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

Server Sent Events util

A simple set of transformation functions to abstract away the sse protocol.

npm Version GitHub package.json version GitHub Downloads Per Month GitHub last commit

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.

Docs