event-sender
v0.1.3
Published
Provide an EventSender prototype to send Server Sent Events (sse), meant to be consumed by the EventSource interface.
Downloads
5
Maintainers
Readme
event-sender
Provide an EventSender
prototype to send Server Sent Events, meant to be consumed by the EventSource interface.
Installation
npm install --save event-sender
Usage
Initialize your EventSender
object with an HTTP response object (the ExpressJS res
parameter, for instance):
var EventSender = require('event-sender');
var event = new EventSender(response);
Then you can use its send()
method to send events to the client:
// Send plain text to the client:
event.send({ name: 'greeting', data: 'Hello, World!\nHow the hell are yer?!' });
// Send JSON data to the client:
event.send({
name: 'direct-message',
data: { author: 'cGuille', content: 'Hope you like it!' }
});
Non string data are JSON stringified.
It is also possible to provide a callback which will be called when the data have been sent to the client :
event.send(eventObj, function () {
console.log('The event has been flushed to the network.');
});
Licence
This small library is distributed under the MIT licence.