ngn-sse

0.0.3 • Public • Published

ngn-sse

A server-sent events implementation for node.js. Adapted from einaros/sse.js. Supports CORS.

js-standard-style

Code Climate

Install

npm install ngn-sse

Basic Node.js Server

let SSE = require('ngn-sse')
let http = require('http')
 
let server = http.createServer(function(req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'})
  res.end('okay')
});
 
server.listen(8080, '127.0.0.1', function() {
  let sse = new SSE(server)
  sse.on('connection', function(client) {
    client.send({
      event: topic, 
      data: payload
    })
  })
})

Options for client.send:

{
  event: 'topic', // Optional
  data: {         // This can also be a string/number
    my: 'data'
  },
  id: 'optional', // Auto-generated if not defined
  retry: 'optional' // Optional retry data
}

Basic Client

var es = new EventSource("/sse")
 
// Handle any message
es.onmessage = function (event) {
  console.log(event.data)
}
 
// Handle a specific event type/topic
es.addEventListener('topic', function(e){
  console.log(e.data)
})

Readme

Keywords

Package Sidebar

Install

npm i ngn-sse

Weekly Downloads

0

Version

0.0.3

License

MIT

Last publish

Collaborators

  • cbutler