expresse
v1.0.2
Published
Simple SSE server middleware for Express
Downloads
102
Readme
Simple SSE Server
Simple middleware that will leave the HTTP connection and use it for SSE communication.
Below is an example of setting up the server on the /events
route.
var SSEServer = require('expresse');
var sse = new SSEServer('events');
app.get('/events', function(req, res) {
sse.connect(req, res);
sse.broadcast('greeting', {message: 'hello world'});
});
Client code is simple too!
var events = new EventSource('http://localhost:4000/events');
events.addEventListener('greeting', function(event) {
var data = JSON.parse(event.data);
console.log('greeting: '+ data);
});
events.onopen = function(e) {
//called when socket is listenning
}
events.onerror = function(e) {
events.close();
}