Creating the Socket.io events

Now is the time to create the socket.io event issues in our server. The socket.io allows you to send and receive events with any type of data:

Open app.js from the root folder and add the following lines of code at the end of the file:

 // Starting with socket.io var io = require('socket.io').listen(server); // Create an Array to hold users var userList = []; // Create an Array to hold connections var connections = []; // Start connection listener io.sockets.on('connection', function (socket) { connections.push(socket); console.log("Connected:", connections.length ); // Setup Disconnect user socket.on('disconnect', function (data) { if (socket.username) { userList.splice(userList.indexOf(socket.username), 1); updateUsernames(); ...

Get Node.js 6.x Blueprints now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.