You are previewing Building Node Applications with MongoDB and Backbone.

Building Node Applications with MongoDB and Backbone

Cover of Building Node Applications with MongoDB and Backbone by Mike Wilson Published by O'Reilly Media, Inc.

Chapter 9. Chat

The social networking application is off to a good start. It’s possible to share your updates, add and find contacts, and interact with each other all from piecing together some of the useful libraries that are already available for Node.js and Backbone. But if you were to try to use this application in real life, you might find that it feels cold and sterile—it’s certainly a networking site, but it isn’t very social.

It’s time to brighten up the environment by enhancing your features with real-time capabilities. First up: chat!

Online chat presents your users with a means to communicate with one another almost as quickly as if they were having a real conversation. During a chat, the goal of your application should be to deliver messages from one party to the other as quickly as possible, without letting other processes like logging or database retrieval get in the way. A good chat system is really little more than a conduit between two communicators.


On the Node.js side of things, the application is starting to get too large to reasonably handle in a single script, so you’re going to pull all the authentication- and account-related routes out of app.js and move them into their own files. Then you’re going to loop through all of the routes during startup and add them to your application, as shown in Example 9-1.

Example 9-1. Moving routes to their own files

fs.readdirSync('routes').forEach(function(file) {
  var routeName = file.substr(0, file.indexOf('.')); ...

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