Database Reference
In-Depth Information
if(e.readyState != EventSource.CLOSED) {
if(window.console) console.log("SSE error");
} else
if(window.console) console.log("closed SSE
connection");
});
Finally, the counters event is handled to distribute the counter state to the
already established listeners:
events.addEventListener('counters',function(e) {
var data = JSON.parse(e.data);
for(counter in data)
$(document).trigger('data:'+counter,data[counter]);
});
});
To respond to this event, the Express application defines a route
corresponding to the endpoint used to create the EventSource object,
using the SSE middleware function to provide the SSE protocol added
dashboard/index.js :
app.get('/api/v1/dashboard', sse(), routes.dashboard);
The Server Sent Events middleware is a fairly simple piece of code,
implemented in dashboard/lib/sse/index.js . This particular
implementation maintains unique message IDs for events on a per-event
type basis as well as allowing for a default event name:
module.exports = function(options) {
options = options || {};
return function sse(req,res,next) {
res._counts = {};
res._nextId = function(type) {
type = type || "message";
count = (this._counts[type] || 0) + 1;
this._counts[type] = count;
Search WWH ::




Custom Search