Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Hour 12. Introducing Socket.IO > Basic Socket.IO Example

Basic Socket.IO Example

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/');


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint