https://github.com/lsongdev/server-sent-events
server send events library for node.js
https://github.com/lsongdev/server-sent-events
eventsource http realtime server-push sse
Last synced: 7 months ago
JSON representation
server send events library for node.js
- Host: GitHub
- URL: https://github.com/lsongdev/server-sent-events
- Owner: lsongdev
- License: mit
- Created: 2017-09-06T09:51:00.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2020-01-09T03:50:12.000Z (about 6 years ago)
- Last Synced: 2025-08-19T02:57:53.837Z (7 months ago)
- Topics: eventsource, http, realtime, server-push, sse
- Language: JavaScript
- Homepage: https://npmjs.org/server-send-events
- Size: 2.93 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## server-send-events
> server send events library for node.js
[](https://npmjs.org/server-send-events)
### Installation
```bash
$ npm install server-send-events
```
### Example
```js
const http = require('http');
const EventSource = require('server-send-events');
const es = new EventSource();
const server = new http.Server();
const send = (res) => res.end(`
var source = new EventSource('/events');
source.onmessage = function(e) {
document.body.innerHTML = e.data;
console.log(e.data);
};
`);
server.on('request', (req, res) => {
if(es.match(req, '/events')){
es.handle(req, res);
}else{
send(res);
}
})
server.listen(3000, err => {
if(err) throw err;
console.log(`server-send-events is running at http://localhost:${server.address().port}`);
setInterval(() => {
if(es) es.send(`Current time is : ${new Date().toLocaleString()}`);
}, 1000);
});
```
### API
- send
- event
- retry
### Contributing
- Fork this Repo first
- Clone your Repo
- Install dependencies by `$ npm install`
- Checkout a feature branch
- Feel free to add your features
- Make sure your features are fully tested
- Publish your local branch, Open a pull request
- Enjoy hacking <3
### MIT
This work is licensed under the [MIT license](./LICENSE).
---