triblondon / node-sse-pubsub

Server sent events for NodeJS
MIT License
71 stars 16 forks source link

Server-sent events for NodeJS

A simple NodeJS module to generate Server-Sent-Events streams with a publish/subscribe interface and simple integration with either Node's built in HTTP library or any framework that exposes it, eg. ExpressJS.

Usage

Install with npm i sse-pubsub, then, if you are using Express:

const express = require('express');
const SSEChannel = require('sse-pubsub');

const app = express();
const channel = new SSEChannel();

// Say hello every second
setInterval(() => channel.publish("Hello everyone!", 'myEvent'), 1000);

app.get('/stream', (req, res) => channel.subscribe(req, res));

app.listen(3000);

You should now be able to visit http://localhost:3000/stream and see a sequence of 'Hello everyone!' events appearing once per second.

To subscribe to the stream from client-side JavaScript:

const es = new EventSource("/stream");
es.addEventListener('myEvent', ev => {
    alert(ev.data);
});

A more detailed demo lives in /demo/ and can be run with npm start if the module is checked out for development (see development).

API

SSEChannel(options) (constructor)

Creates a new SSEChannel. Available options are:

const channel = new SSEChannel({
    pingInterval: 10000,
    startId: 1330
});

subscribe(req, res, [events])

Attaches an inbound HTTP request to an SSE channel. Usually used in conjuction with a framework like Express. Returns a reference for the client.

const channel = new SSEChannel();
app.get('/stream', (req, res) => channel.subscribe(req, res));

publish(data, [eventName])

Publishes a new event to all subscribers to the channel.

Since all events published to a channel will be sent to all subscribers to that channel, if you want to filter events that are of interest to only a subset of users, it makes sense to use multiple separate channel instances. However, eventName is useful if you are sending events that are all relevant to all subscribers, but might need to be processed by different client-side handlers. Event names can therefore be considered to be like method names of a method that you are invoking in the client-side code.

Returns the ID of the new message.

unsubscribe(clientRef)

Detaches an active HTTP connection from the channel.

close()

Closes all subscriber connections, deletes message history and stops ping timer. If you intend to create temporary channels, ensure you close them before dereferencing, otherwise timers and HTTP clients may cause memory leaks.

listClients()

Returns an object where the keys are the remote addresses of each connected client, and the values are the number of connections from that address.

console.log(channel.listClients());
// {
//  '::ffff:127.0.0.1': 2
// }

getSubscriberCount()

Returns the number of currently connected clients.

Development

To develop on the project, clone the repo and run npm install. Then if you want to run the demo server:

  1. npm start
  2. Open http://127.0.0.1:3101 in your browser

To run the tests, npm test. The project uses Express for the demo server and Mocha and Chai for the tests, however none of these are needed in production. The tests use Node's raw http library rather than express to ensure there is not any accidental dependence on request or response properties added by Express.

Licence

MIT.