Book Image

Building Scalable Apps with Redis and Node.js

By : Joshua Johanan
Book Image

Building Scalable Apps with Redis and Node.js

By: Joshua Johanan

Overview of this book

Table of Contents (17 chapters)
Building Scalable Apps with Redis and Node.js
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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', {username: socket.username, room: data.room});
  });
  socket.on('privatePing', function(data){
    io.sockets.connected[data.socket].emit('ping', {username: socket.username, priv: true, room: data.room});
  });
}...