Node.js transform stream for parsing server-sent events
npm install @server-sent-stream/nodeThis package allows you to consume server-sent events through Node's stream API.
This package can be used as an ESM or CommonJS module:
``js`
import EventSourceStream from '@server-sent-stream/node';
`js`
const EventSourceStream = require('@server-sent-stream/node');
The EventSourceStream is a Node stream.Transform. It consumes a stream of binary data (e.g. Buffers or Uint8Arrays), and produces a stream of MessageEvents.
You can use it with any Node Readable stream, like the kind returned by node-fetch:`js
// Fetch some URL that returns an event stream
const response = await fetch('https://example.com/events', {body: '...'});
// Pipe the response body into an EventSourceStream
const decoder = new EventSourceStream();
decoder.on('data', message => {
// The value will be a MessageEvent.
console.log(message);
// MessageEvent {data: 'message data', lastEventId: '', …}
})
response.body.pipe(decoder);
``