🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Sign inDemoInstall
Socket

sse-express

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sse-express

Server sent events middleware implementation for expressjs

1.0.2
latest
npm
Version published
Maintainers
1
Created
Source

SSE express :satellite:

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

Keywords

express

FAQs

Package last updated on 25 May 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts