Database Reference
In-Depth Information
return count;
}
When a connection begins, it must check that the call is coming from a
source that supports SSE. If this is the case, it sends the appropriate content
type and other settings to start the connection:
if(req.accepts("text/event-stream")) {
req.setTimeout(Infinity);
res.writeHead(200,{
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keepalive'
});
} else {
res.writeHead(200,{
'Content-Type': 'application/json',
'Cache-Control': 'no-cache',
'Connection': 'keepalive'
})
}
Before continuing along the chain, the middleware adds functions to the
response object to transmit SSE events in the appropriate format:
res.event = function(event,type,opt) {
opt = opt || {};
type = type || options.event;
if(type)
this.write("event:"+type+"\n");
this.write("id:"+this._nextId(type)+"\n");
if(opt.retry)
this.write("retry:"+opt.retry+"\n");
this.write("data:"+event+"\n\n");
}
res.json = function(json,type,opt) {
this.event(JSON.stringify(json),type,opt);
}
next();
Search WWH ::




Custom Search