A Server-Sent Events transform stream.
$ npm install segmentio/sse-stream
Transform a random object stream into sse format and stream it over http:
var http = require('http');
var sse = require('sse-stream');
var Readable = require('stream').Readable;
http.createServer(function(req, res){
objectStream().pipe(sse()).pipe(res);
}).listen(3000);
function objectStream(){
var stream = new Readable({ objectMode: true });
var i = 0;
stream._read = function(){
this.push({
foo: ++i
});
if (i == 3) this.push(null);
}
return stream;
}
$ curl http://localhost:3000
data: {"foo":1}
data: {"foo":2}
data: {"foo":3}
$
Create a new stream that transforms data into the server-sent events format. It accepts (multiline) strings, numbers, buffers and objects as inputs. error
events are emitted when functions or circular object structures are written to it.
Options:
retry:number
How many milliseconds the browser should wait in between reconnects
MIT.