https://github.com/dpskvn/express-sse
An Express middleware for quick'n'easy server-sent events.
https://github.com/dpskvn/express-sse
express-middleware express-sse javascript nodejs sse
Last synced: 2 months ago
JSON representation
An Express middleware for quick'n'easy server-sent events.
- Host: GitHub
- URL: https://github.com/dpskvn/express-sse
- Owner: dpskvn
- License: mit
- Created: 2014-02-01T01:58:59.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2023-11-19T01:46:18.000Z (over 1 year ago)
- Last Synced: 2024-04-14T12:30:11.399Z (about 1 year ago)
- Topics: express-middleware, express-sse, javascript, nodejs, sse
- Language: JavaScript
- Homepage: https://npmjs.org/package/express-sse
- Size: 278 KB
- Stars: 101
- Watchers: 6
- Forks: 26
- Open Issues: 23
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
express-sse
============[](https://badge.fury.io/js/express-sse) [](https://travis-ci.org/dpskvn/express-sse) [](https://codeclimate.com/github/dpskvn/express-sse) [](https://codecov.io/gh/dpskvn/express-sse)
[](https://nodei.co/npm/express-sse/)
An Express middleware for quick'n'easy server-sent events.
## About
`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.## Installation:
`npm install --save express-sse`or
`yarn add express-sse`
## Usage example:
### Options:
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' });
```### Server:
```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"]);
```### Client:
```js
var es = new EventSource('/stream');es.onmessage = function (event) {
...
};es.addEventListener(eventName, function (event) {
...
});
```