Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Socket.IO works on both the server and the client. To use it, you must add it to your server-side JavaScript (Node.js) and your client-side JavaScript (jQuery, etc.). This is because communication can be both ways, so Socket.IO needs to work on both sides. In the following example, you learn about building the server side first. In Listing 12.1, Node.js is used to serve a single HTML file.
Listing 12.1. Serving a File with Node.js
var http = require('http');
var fs = require('fs');
var server = http.createServer(function (req, res) {
fs.readFile('./index.html', function(error, data) {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(data, 'utf-8');
});
}).listen(3000, "127.0.0.1");
console.log('Server running at http://127.0.0.1:3000/');