Skip to content

EventSource/node-ssestream

Repository files navigation

SseStream

A zero-dependency node stream for writing Server-Sent Events.

Installation

npm install ssestream

Or:

yarn add ssestream

Usage

In a (req, res) handler for a request event, Express #get route or similar:

const SseStream = require('ssestream')

function (req, res) {
  const sse = new SseStream(req)
  sse.pipe(res)
  
  const message = {
    data: 'hello\nworld',
  }
  sse.write(message)
}

Properties on message:

  • data - String or object, which gets turned into JSON
  • event - (optional) String
  • id - (optional) String
  • retry - (optional) number
  • comment - (optional) String

TypeScript

The SseStream#writeMessage(message) method is a type-safe alias for SseStream#write(message).

About

Send Server-Sent Events with a stream

Resources

License

Stars

Watchers

Forks

Packages

No packages published