Database Reference
In-Depth Information
written it has to be enabled with a startup switch, so there is effectively no
support for this “in the wild.”
Because the protocol is more complicated than Server Sent Events, the
dashboard application relies on a library for WebSocket support, rather
than just implementing the server and client directly. There are several
WebSocket libraries available for Node, but one of the more popular
libraries is socket.io , which provides both a server-side library as well as
a client-side library with identical interfaces. It also standardizes browser
API differences to provide a common interface on all platforms. The easiest
waytouse socket.io istoattachitsserverinterfacedirectlytotheExpress
applicationandtreatitas,essentially,aseparateinterfacetotheapplication.
To do this requires creating an http.Server object that replaces the
listen() method on the application. In the dashboard example, this
involves creating the server object when the application is initialized in
dashboard/index.js :
var express = require('express')
, routes = require("./routes")
, path = require('path')
, app = express()
, server = require('http').createServer(app)
, sse = require('./lib/sse')
;
Then, app.listen(argv.port) is replaced with
server.listen(argv.port) and the socket.io code is attached to
the server after the application has been initialized:
server.listen(argv.port);
var ws = require('socket.io').listen(server);
ws.sockets.on('connection',function(socket) {
function update(counter,value) {
var msg = {};msg[counter] = value;
socket.emit('counters',msg);
}
socket.emit('counters',counters.state);
counters.on('updated',update);
socket.on('disconnect',function() {
Search WWH ::




Custom Search