Skip to content

heroku/sseasy

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

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

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

Stars

Watchers

Forks

Packages

No packages published