1.0.5 • Published 2 years ago

@server-sent-stream/node v1.0.5

Weekly downloads
-
License
ISC OR MIT OR Apa...
Repository
github
Last release
2 years ago

@server-sent-stream/node

This package allows you to consume server-sent events through Node's stream API.

Usage

This package can be used as an ESM or CommonJS module:

import EventSourceStream from '@server-sent-stream/node';
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:

// 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);

Related packages

If you want a streaming interface for the Web Streams API, so you can use this in the browser, see @server-sent-stream/web. For just the event stream parser, see @server-sent-stream/parser.

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago