O'Reilly logo

Building Scalable Apps with Redis and Node.js by Joshua Johanan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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', ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required