Skip to content
master
Go to file
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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

About

SSE middleware for connect

Resources

Packages

No packages published
You can’t perform that action at this time.