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 = ;//...app;
At the client side you can listen to message through EventSource
instance:
let eventSource = 'http://localhost:80/updates'; eventSource;
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.
EventSource
Useful references about
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