Skip to content

heroku/sseasy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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