Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/thibauts/node-sse-emitter
Server-Sent Events as simple as they can get
https://github.com/thibauts/node-sse-emitter
Last synced: about 2 months ago
JSON representation
Server-Sent Events as simple as they can get
- Host: GitHub
- URL: https://github.com/thibauts/node-sse-emitter
- Owner: thibauts
- License: mit
- Created: 2014-10-24T14:29:18.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2014-10-25T13:10:32.000Z (about 10 years ago)
- Last Synced: 2024-08-29T18:37:01.805Z (4 months ago)
- Language: JavaScript
- Size: 125 KB
- Stars: 6
- Watchers: 4
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
sse-emitter
===========
### Server-Sent Events as simple as they can getBind an instance to a route and emit events named after the matched path.
For example, if you bind to route `/hello`, when you emit event `/hello`, browsers listening on this endpoint will get the event data.
If you bind to route `/hello/:name`, you can send messages to browsers having `EventSource('/hello/world')` and `EventSource('/hello/sse')` by emitting respectively `/hello/world` and `/hello/sse` events. This makes implementing chatroom-like or channel-like functionnality a breeze. See usage.
Of course a single emitter can be bound to as many routes as you like. Also, the `bind` method returns a simple handler and doesn't prevent you from adding middleware, eg. for authentication.
Installation
------------```bash
$ npm install sse-emitter
```Usage
-----```javascript
var express = require('express');
var SSE = require('sse-emitter');var sse = new SSE({
keepAlive: 30000, // in ms, defaults to 10000
});var app = express();
app.get('/channel/:id', sse.bind());
app.listen(5000);
setInterval(function() {
sse.emit('/channel/1', {
text: "Hello, SSE !"
});
}, 1000);```
Then browser-side :
```javascript
var channel = new EventSource('/channel/1');
channel.addEventListener('message', function(ev) {
console.log(JSON.parse(ev.data));
});
```