node package manager
It’s your turn. Help us improve JavaScript. Take the 2017 JavaScript Ecosystem Survey »



This is a fork of sse-stream that adds cookie-based authentication support & dynamic connection creation

See for an example of using auth and subscribing to multiple channels that you've created dynamically.

Expose HTML5 Server Sent Events as an installable appliance on Node.JS http servers; connections are emitted as Writable streams.

var http = require('http')
  , fs = require('fs')
  , through = require('through')
  , sse = require('sse-stream-auth')({ prefixes: ['/awesomeApp'], create: true }) // Allow dynamic connection creation 
  , serv
module.exports = serv = http.createServer(function(req, resp) {
  resp.setHeader('content-type', 'text/html')
  resp.end('<html><body><script type="text/javascript">('+js+')()</script></body></html>')
sse.on('connection', function(client) {
    .pipe(through(function(buf) { this.emit('data', buf.toString()) }))
// client-side code: 
function js() {
  var es = new EventSource('/awesomeApp/someChannelNameHere')
    , pre = document.createElement('pre')
    , closed = false
  es.onmessage = function(ev) {
    if(closed) return
    window.scrollTo(0, pre.clientHeight)
  es.addEventListener('end', function() {
    closed = true
  }, true)
  es.onerror = function(e) {
    closed = true


sse = require('sse-stream-auth')(options)

Create a SSE server that emits connection events on new, successful eventstream connections.

The argument must be an options object:

{ prefixes: ['/awesomeApp', '/awesomeUsername']
, keepalive: 1000 }

keepalive determines the interval time in ms that keepalives will be sent to all connected clients.

sse.on('connection', function(client))

client is a writable stream representing a client connection (request response pair).

Of note, all data sent through this connection will be stringified before sending due to the event stream spec.

client.retry(integer ms)

Send a "retry" message that lets the client know how many MS to wait until retrying a connection that ended.