O'Reilly logo

CoffeeScript Programming with jQuery, Rails, and Node.js by Michael Erasmus

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

Joining a list

To join a list, we'll use a feature of Socket.IO called rooms . It allows the Socket.IO server to segment its clients and emit messages to subsets of all the connected clients. On the server, we'll keep track of the to-do lists of each room and then tell the client to sync its local list when connected.

We'll update the app.coffee file with the highlighted code shown in the following code snippet:

@todos = {}
io.sockets.on 'connection', (socket) =>
  console.log('connected')
  socket.on 'joinList', (list) =>
    console.log "Joining list #{list}"
    socket.list = list
    socket.join(list)
    @todos[list] ?= []
    socket.emit 'syncItems', @todos[list]

We initialize the @todos instance variable to be an empty hash. It will hold the to-do lists for ...

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