sse-emitter
Server-Sent Events as simple as they can get
Bind an instance to a route and emit events named after the matched path.
For example, if you bind to route /hello
, when you emit event /hello
, browsers listening on this endpoint will get the event data.
If you bind to route /hello/:name
, you can send messages to browsers having EventSource('/hello/world')
and EventSource('/hello/sse')
by emitting respectively /hello/world
and /hello/sse
events. This makes implementing chatroom-like or channel-like functionnality a breeze. See usage.
Of course a single emitter can be bound to as many routes as you like. Also, the bind
method returns a simple handler and doesn't prevent you from adding middleware, eg. for authentication.
Installation
$ npm install sse-emitter
Usage
var express = ;var SSE = ; var sse = keepAlive: 30000 // in ms, defaults to 10000 encoder: JSONstringify // this is the default anyway, but could be btoa(), ...; var app = ; app; app; ;
Then browser-side :
var channel = '/channel/1';channel;