Database Reference
In-Depth Information
Adding Streaming to Web Applications
With the dashboard counters ready for streaming data, there are two viable
web options for web-based delivery of data: SSEs and WebSockets. Both
technologies enjoy fairly broad support across modern browsers, including
mobile browsers. Both have their pros and cons making the choice of a
particular technology more a matter of preference than requirements.
This section covers how to use both technologies to communicate with the
back-end server. The two technologies do not interfere with each other, so it
is perfectly reasonable to provide back-end support for both styles.
Server-Side Counter Management
Beforeintroducingthecommunicationtechnologies,thecounterstobeused
in the dashboard must be maintained on the server side. Otherwise, there
will be nothing to send to the client after it is wired to the server! In this
example application, counters are maintained in a singleton object derived
from Node's built-in EventEmitter class with the implementation found
in dashboard/lib/counters/index.js :
var events = require('events')
, util = require('util')
;
function Counter() {
//Returns the singleton instance if it exists
if(arguments.callee.__instance) {
return arguments.callee.__instance;
}
arguments.callee.__instance = this;
this._state = { };
events.EventEmitter.call(this);
}
util.inherits(Counter,events.EventEmitter);
module.exports = new Counter();
The singleton instance is stored in the arguments.callee.__instance
variable. This allows the counter object to be imported with a require
statement.
Search WWH ::




Custom Search