backbone-dnode  

backbone-dnode is a real-time multi-channel chat application for Node.js.

The complete annotated source code is also available.

Introduction | Installation | Usage |

Introduction

Backbone-DNode is a server to client integration package for use with, you guessed it, Backbone and DNode. The package brovides both node.js server side code for CRUD and Pubsub routines, as well as the matching client (or server) side routines.

The idea is to make writing a real-time Backbone application as simple as possible, the app is supported on the server side by using the Mongoose ORM for final validation and persistence.

The pubsub mechanics will default to using socket.io for updating the connected clients, however, you can pass a redis server and connection options to the pubsub `config` method to utilize the built in redis publish and subscribe methods.

Installation

backbone-dnode depends on Node.js and the Node Package Manager (npm). If you don't already have these installed, grab the latest. Node releases can be found on the download page. NPM can be installed with a script:
curl http://npmjs.org/install.sh | sudo sh

Install backbone-dnode via NPM:
npm install backbone-dnode

The project can be installed via NPM, or by cloning this repo into your project.

    
      npm install backbone-dnode  
    

or

    
      git clone git://github.com/sorensen/backbone-dnode.git
    

Usage

There are two parts to using the backbone-dnode package, one part is to be used on the server, the other the client.

Server usage

Whip up a server and attatch DNode, while using the backbone-dnode methods as middleware.

    
      var express    = require('express'),
          dnode      = require('dnode'),
          middleware = require('backbone-dnode'),
          browserify = require('browserify'),
          server     = express.createServer();
    

Bundle the client side support files with browserify, then attatch it to the express server instance.

    
      var bundle = browserify({
          require : 'backbone-dnode',
          mount   : '/backbone-dnode.js',
      });
      
      server.use(bundle);
    

Register your Mongoose schemas, and then pass the database instance to the CRUD configuration. At least one mongoose schema must be registered to use the CRUD routines.

    
        var Mongoose = require('mongoose'),
            Schema   = mongoose.Schema,
            ObjectId = Schema.ObjectId;

        Mongoose.connect('mongodb://localhost/db');

        Foo = new Schema({
            bar : { type : String, index : true }
        });

        database = Mongoose.connect('mongodb://localhost/db');
        middleware.crud.config(database, function() {
            // Placeholder
        });
    

Configure the Redis connection if you would like to use Redis as the pubsub mechanics. This will allow you to use other libraries such as Cluster, letting Redis act as the message queue.

    
        var redis     = require('redis'),
            publish   = redis.createClient(),
            subscribe = redis.createClient();
        
        middleware.pubsub.config(publish, subscribe, function() {
            // Placeholder
        });
    

Client usage

Include DNode and the browserified bundle, then attach the methods to the DNode instance.

    
        <script src="underscore.js"></script>
        <script src="backbone.js"></script>
        <script src="dnode.js"></script>
        <script src="backbone-dnode.js"></script>
    
    
      var middleware = require('backbone-dnode');
      
      DNode()
          .use(middleware.crud)
          .use(middleware.pubsub)
          .connect(function(remote) {
          
              middleware.crud.config(remote);
              middleware.pubsub.config(remote);
              
          });
    

To connect to node.js and mongoose from the browser (or on the server), a model `type` for mongoose must be specified, as well as overriding the `sync` method on each model, an underscore mixin has been created to provide optional support based on the model, in case you have different persistant support in mind.

    
      foo = Backbone.Model.extend({
          type : 'room',
          sync : _.sync
      })
    

You can also override the sync method globally, by overriding the default `Backbone.sync` method

    
      Backbone.sync = _.sync
    

Once the middleware has been established, and a model has been set to use it (or if as been overridden globally), the default Backbone methods will automatically send the changes through the socket (dnode), where they will be mapped to the corresponding Mongoose schema, and then published to the connected clients that have been subscribed to the model or collection's URL.