Adding rooms

We will use our current app as the basis since most of it can be reused. Copy namespaces.js and namespace.html and create rooms.js and rooms.html. Open up rooms.js and get rid of the namespace connection listener, as we are only using rooms here. Then, we will modify the normal connection and add our room-specific elements to it. Your rooms.js should look like the following code:

var io = require('socket.io').listen(4000); io.sockets.on('connection', function(socket){ socket.on('join', function(data){ socket.username = data.username; socket.join(data.room); socket.broadcast.to(data.room).emit('join', {username: data.username, socket: socket.id, room: data.room}); }); socket.on('ping', function(data){ socket.broadcast.to(data.room).emit('ping', ...

Get Building Scalable Apps with Redis and Node.js 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.