Before you add Socket.IO to your current PacktChat app, let's first build a couple of very simple apps. This will first allow us to understand what we are doing, and then build on it.
The first thing, of course, is to get the package from npm. We will do this exactly like we did in the last chapter, by adding all the packages to package.json
and running npm install
.
Socket.IO has quite a few dependencies, so this may take a minute or two. Once that is done, you can create your first app. Create a directory named first app
, create an app.js
file, and add the following code in it:
var io = require('socket.io').listen(4000); io.sockets.on('connection', function(socket){ socket.emit('ping'); socket.on('pong', function(data){ console.log('pong'); }); });
Since Socket.IO is event driven, we start off by listening for a connection event. Do not think of this in terms of HTTP actions as it does not map to any. While we will run them on...