Skip to content
forked from einaros/sse.js

Server-Sent Events made easy for node.js

Notifications You must be signed in to change notification settings

jlam55555/sse.js

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sse.js

Forked from @einaros/sse.js

The base module set up its own http listeners. This is a simplified version that does not set up its own listeners. Express middleware can be written to invoke the handleRequest method.


Example Usage

The purpose of creating this fork is that the base module creates its own middleware and cannot be used easily with express, especially in conjunction with other express middleware like express-session. Session data from express-session can now be accessed

Here is an example usage of sse.js as middleware.

server.js

// set up express and express-session
const express = require('express');
const app = express();
const expressSession = require('express-session');
app.use(require('express-session')({ /* express-session options */ }));
app.listen(8080, _ => {});

// sse
const sse = require('sse')();

// now you can write express middleware for sse.js!
// this sends express data
app.use((req, res, next) => {
  if(req.path === '/sse') {

    // note that handleRequest's third param is now for data that will be assigned to the `data` property of client
    sseServer.handleRequest(req, res, req.session);
  } else {
    next();
  }
});

// note that sseServer doesn't require any params now
let sseServer = new sse();
sseServer.on('connection', client => {

  // you can access client details now! woohoo!
  console.log('express session id: ' + client.data.id);
});

About

Server-Sent Events made easy for node.js

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%