SSE middleware for connect
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
test
.gitignore
.jshintignore
.jshintrc
.travis.yml
CHANGELOG.md
README.md
index.js
package.json

README.md

SSEasy

Build Status

NPM

Server sent events middleware for Connect & Express.

All messages in a single connection are sent with incrementing IDs. If the client passes an ID in a last-event-id header, the middleware ignores messages until that ID is reached.

Use

On the server as a writeable stream:

var sse = require('sseasy');
var fs  = require('fs');

app.get('/stream', sse(), function(req, res) {
  fs.createReadStream('/some/file.txt').pipe(res.sse);
});

On the server with manual events:

var sse = require('sseasy');

app.get('/stream', sse(), function(req, res) {
  res.sse.write('a message');
  setTimeout(function() {
    res.sse.write('a second message');
  }, 2000);
});

On the client (initial connection):

var source = new EventSource('/stream');
source.addEventListener('message', function(ev) {
  console.log('id: ', ev.lastEventId);
  console.log('message: ', ev.data);
});

// id: 0
// message: a message

// id: 1
// message: a second message

On the client (reconnect sending last-event-id: 0 header ):

var source = new EventSource('/stream');
source.addEventListener('message', function(ev) {
  console.log('id: ', ev.lastEventId);
  console.log('message: ', ev.data);
});

// (message 0 was skipped)
// id: 1
// message: a second message