sse-express

1.0.2 • Public • Published

SSE express 📡

Server Sent Events middleware implementation for express

Install

npm install --save sse-express

The package requires express, because it was created directly for the framework. Also it utilizes ES6 features, so be sure that node v5.0+ is installed.

Usage

Use it simply a middleware for any of your routes. When is used as middleware method sse() is added to response object of the route. You can use it to send messages to a client.

let sseExpress = require('./sse-express');
//...
app.get('/updates', sseExpress, function(req, res) {
    res.sse('connected', {
      welcomeMsg: 'Hello world!'
    });
});

At the client side you can listen to message through EventSource instance:

let eventSource = new EventSource('http://localhost:80/updates');
 
eventSource.addEventListener('connected', (e) => {
    console.log(e.data.welcomeMsg);
    // => Hello world!
});

Important! Don't forget to check out browser compatibility of EventSource. At the moment it doesn't implemented in any versions of IE.

Every 3 seconds a handshake message will be sent to the client to not allow a browser lose a connection. Handshake message is just a comment that will not be caught by any of EventSource's events.

Useful references about EventSource

API

res.sse(evt, json, id)

evt - is event name json - object that will be sent as json string to a client [id] - optional id of event

MIT License

Package Sidebar

Install

npm i sse-express

Weekly Downloads

407

Version

1.0.2

License

MIT

Last publish

Collaborators

  • alizurchik