Server-sent events (SSE) module for NodeJS.
Because this package is not in npmjs.org yet, it needs to be installed from this Github repo:
$ npm install --save arnellebalane/arcus
arcus
inherits the prototype of EventEmitter
.
-
connect(req, res) => id
: Enable Server-Sent Events (SSEs) on the given connection.req
: anhttp.IncomingMessage
instanceres
: anhttp.ServerResponse
instance- returns an
id
, which is a uuid that identifies the connection
-
disconnect(id)
: Send thedisconnect
event to the SSE client identified by the givenid
and then forget about it.id
: The uuid of the SSE connection to be disconnected
-
send(id, options={})
: Send data to an SSE client identified by the given uuid.id
: The uuid of the SSE client to send the data to.options.event
: The name for the server-sent event. Specifying this option will trigger the client-side EventSource's event listeners for the event of the same name, otherwise themessage
event.options.data
: The data that will be sent to the SSE client, and can be obtained from "e.data" in the client-side callbacks.
-
event: 'connect'
: Emitted after successfulconnect()
operation. Callback function receives the following arguments:id
: The uuid that identifies the new SSE connection
-
event: 'disconnect'
: Emitted after successfuldisconnect()
operation. Callback function receives the following arguments:id
: The uuid of the SSE connection that got disconnected
-
event: 'send'
: Emitted after successfulsend()
operation. Callback function receives the following arguments:id
: The uuid of the SSE connection where the data was sent to.options
: Same options object that got passed to thesend
function.
const arcus = require('arcus');
arcus.on('connect', (id) => {
console.log(`Client "${id}" is now connected.`);
arcus.send(id, 'You are now connected to Arcus!');
});
app.get('/stream', (req, res) => arcus.connect(req, res));
MIT License