server-send-events

0.0.8 • Public • Published

server-send-events server-send-events

eventsource

Installation

$ npm install server-send-events

Example

const http        = require('http');
const EventSource = require('server-send-events');
 
const es = new EventSource();
const server = new http.Server();
const send = (res) => res.end(`<script>
  var source = new EventSource('/events');
  source.onmessage = function(e) {
    document.body.innerHTML = e.data;
    console.log(e.data);
  };
</script>`);
 
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.


Package Sidebar

Install

npm i server-send-events

Weekly Downloads

11

Version

0.0.8

License

MIT

Unpacked Size

7.46 kB

Total Files

7

Last publish

Collaborators

  • song940