Socket
Socket
Sign inDemoInstall

sse-express

Package Overview
Dependencies
64
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    sse-express

Server sent events middleware implementation for expressjs


Version published
Weekly downloads
258
decreased by-36.76%
Maintainers
1
Install size
1.64 MB
Created
Weekly downloads
 

Readme

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

FAQs

Last updated on 25 May 2017

Did you know?

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc