Database Reference
In-Depth Information
counters.removeListener('counters',update);
});
});
The implementation itself is very similar to the dashboard endpoint of the
Server Sent Events version. When a change to the counters is made, it is
relayed out to the dashboard to allow it to update the interface.
Implementing the client side is also quite similar to the Server Sent Events
version. First, the socket.io library must be included. Because the library
itself provides both server-side and client-side versions, it automatically
creates an endpoint to serve up the appropriate client-side library at
/socket.io/socket .io.js when the server is attached to the Express
server, as it was earlier. The dashboard HTML then only needs to be
modified slightly to support WebSockets, including the extra script tag:
block scripts
script(src='/socket.io/socket.io.js')
script(src='/javascripts/dashboard.js')
script(src='/javascripts/ws.js')
All that remains, as with the SSE version, is to redistribute the counter
update events when they come in over the WebSockets connection. In this
case, even less code is required because the events have already been
converted back to their JSON form by the socket.io library:
$(document).ready(function() {
var socket = io.connect("/");
socket.on('counters',function(data) {
for(counter in data)
$(document).trigger('data:'+counter,data[counter]);
});
});
Search WWH ::




Custom Search