Programming Socket.io
Probably, the top feature in our web application is its ability to send updates directly from the server to the client in real time. To fully implement this feature, we need to call the model's listenForUpdates()
function and push the resulting data to all connected clients.
At the start of this chapter, you learned about the Socket.io
module, which allows you to send data to clients using WebSockets; so, we're going to use this module to push changes to our users' browsers. You may remember that we initialized the Socket.io
module in the app.js
file, so we're now going to edit the file; in particular, we're going to edit the server.listen()
function:
server.listen(8000, function() { console.log"Server up and listening on port 8000"); model.listenForUpdates(function(data) { if((data.new_val != null) && (data.old_val != null)) { // position update io.emit("updates", data.new_val); } else if((data.new_val != null)...