You are previewing CoffeeScript Programming with jQuery, Rails, and Node.js.

CoffeeScript Programming with jQuery, Rails, and Node.js

Cover of CoffeeScript Programming with jQuery, Rails, and Node.js by Michael Erasmus Published by Packt Publishing
  1. CoffeeScript Programming with jQuery, Rails, and Node.js
    1. Table of Contents
    2. CoffeeScript Programming with jQuery, Rails, and Node.js
    3. Credits
    4. About the Author
    5. About the Reviewers
      1. Support files, eBooks, discount offers and more
      2. Why Subscribe?
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
    8. 1. Why CoffeeScript?
      1. CoffeeScript syntax
      2. CoffeeScript has great function syntax
      3. CoffeeScript handles scope better
      4. CoffeeScript has better object syntax
      5. A few other things CoffeeScript fixes
      6. List comprehensions
      7. Conditional clauses and logical aliases
      8. Array slicing and splicing
      9. Destructuring or pattern matching
      10. => and @
      11. Switch statements
      12. Chained comparisons
      13. Block strings, block comments, and strings
      14. Summary
    9. 2. Running CoffeeScript
      1. The CoffeeScript stack
      2. Node.js and npm
      3. Node.js, npm, and CoffeeScript on Windows
      4. Installing CoffeeScript on a Mac
      5. Installing CoffeeScript on Linux
      6. Building Node.js from source
      7. Using CoffeeScript
      8. The coffee command
      9. Summary
    10. 3. CoffeeScript and jQuery
      1. Finding and changing elements
      2. Utility functions
      3. Ajax methods
      4. Using jQuery
      5. Using CoffeeScript and jQuery in the browser
      6. Our application
      7. Summary
    11. 4. CoffeeScript and Rails
      1. What makes Rails special?
      2. Rails and JavaScript
      3. Rails and CoffeeScript
      4. Installing Rails
      5. Developing our Rails application
      6. Summary
    12. 5. CoffeeScript and Node.js
      1. Node is event-driven
      2. Node is fast and scalable
      3. Node is not Rails
      4. Node and CoffeeScript
      5. "Hello World" in Node
      6. Express
      7. WebSocket
      8. Jade
      9. Our application
      10. Let's get started
      11. Creating a view
      12. node-supervisor
      13. The to-do list view
      14. The client side
      15. Adding collaboration
      16. Creating the collaboration UI
      17. WebSocket on the client
      18. WebSocket on the server
      19. Joining a list
      20. Adding to-do items to a shared list
      21. Removing to-do items from a shared list
      22. Summary
    13. Index

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 file with the highlighted code shown in the following code snippet:

@todos = {}
io.sockets.on 'connection', (socket) =>
  socket.on 'joinList', (list) =>
    console.log "Joining list #{list}"
    socket.list = 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 ...

The best content for your career. Discover unlimited learning on demand for around $1/day.