express-sse
Version:
An Express middleware for Server-Sent Events (EventSource)
58 lines (40 loc) • 1.53 kB
Markdown
express-sse
============
[](https://badge.fury.io/js/express-sse)
[](https://nodei.co/npm/express-sse/)
An Express middleware for quick'n'easy server-sent events.
`express-sse` is meant to keep things simple. You need to send server-sent events without too many complications and fallbacks? This is the library to do so.
`npm install --save express-sse`
or
`yarn add express-sse`
You can pass an optional options object to the constructor. Currently it only supports changing the way initial data is treated. If you set `isSerialized` to `false`, the initial data is sent as a single event. The default value is `true`.
```js
var sse = new SSE(["array", "containing", "initial", "content", "(optional)"], { isSerialized: false, initialEvent: 'optional initial event name' });
```
```js
var SSE = require('express-sse');
var sse = new SSE(["array", "containing", "initial", "content", "(optional)"]);
...
app.get('/stream', sse.init);
...
sse.send(content);
sse.send(content, eventName);
sse.send(content, eventName, customID);
sse.updateInit(["array", "containing", "new", "content"]);
sse.serialize(["array", "to", "be", "sent", "as", "serialized", "events"]);
```
```js
var es = new EventSource('/stream');
es.onmessage = function (event) {
...
};
es.addEventListener(eventName, function (event) {
...
});
```