Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

backbone-server

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

backbone-server

Creates a Backbone.Server object which interfaces between Backbone, Socket.IO and Express.

  • 0.3.9
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
decreased by-95.35%
Maintainers
1
Weekly downloads
 
Created
Source

Backbone-Server

Backbone-Server creates a Server module which pulls together Backbone and Express to bring greater structure to your NodeJS applications.

Backbone.Server can be accessed just like any other Backbone module:

var Backbone = require('backbone-server'); // Note: Backbone-Server relies on the backbone npm package and is not required.

// Current defaults for Backbone.Server
var server = new Backbone.Server({
    'express'        : express.createServer(),   // The actual server handling everything
    'port'           : 8000,                     // The port the server will run on
    'public'         : 'public',                 // The directory for static content
    'routes'         : {},                       // For quick apps, basic routes you can configure upon instantiation
    'socketio'       : false,                    // If set to true, adds socketio support to the server
    'views'          : 'views',                  // The directory where views can be found
    'view engine'    : 'ejs'                     // The engine used to render views
});

Routes

Once you have added a Server, add routes just as you would with Express:

server.get('/post/:id', function(req, res) {
    var id = req.params.id,
        article = articles.get(id);
    
    res.render('single', article);
});

server.post('/post', function(req, res) {
    articles.add(req.params);
});

server.put('/post/:id', function(req, res) {
    var article = article.get(req.params.id);
    
    article.set(req.params);
});

server.delete('/post/:id', function(req, res) {
    articles.remove(params.req.id);    
});

Using Socket.io functions:

To enable socket.io functionality, set the socketio attribute to true upon creation of your new Backbone.Server object:

var server = new Backbone.Server(
    'socketio'     : true          // If set to true, Backbone.Server will enable socket.io functionality
});

This will create an instance of socket.io at the io attribute of your new server object (server.io). You can now add standard socket.io events using the bind and receive methods, like so:

// Socket.IO Functions
server.bind('connection', function() {
    console.log("connected");
    
    var Article = new Backbone.Model(),
        Articles = new Backbone.Collection({
            model: Article
        });
        
        // code to add records to Articles data
    
    server.send('models', Articles.toJSON());
});

In your clientside application, you can receive commands from the server like so (note that backbone-server automatically provides a route to load Underscore and Backbone):

<script src="/backbone-server/backbone.js" type="text/javascript"></script>
<script src="/socket.io/socket.io.js" type="text/javascript"></script>
<script>
    var socket = io.connect('http://localhost');
    
    var Article = Backbone.Model.extend();
    
    var Articles = Backbone.Collection.extend({
        model: Article,
        
        initialize: function() {
            
            // Bind an event to notify when an article has been added to the server
            this.bind('add', function(article) {
                console.log("Article added!");
            });
        }
    });
    
    var articles = new Articles();
    
    socket.on('models', function(data) {
        articles.add(d);   // queues the collection event ('articles added!');
    });
      
</script>

To summarize, in the example above, we've created a Backbone.Server which will push down server side models upon connection, and add them to the client scripts Articles collection.

Starting the Server:

Once you have created a server starting it is easy:

server.start();

Don't see something here you like from Express or Socket.io?

###Target Express directly:

// server.express == Express
server.express

###Target Socket.io directly:

// server.io == socket.io
server.io

FAQs

Package last updated on 04 Dec 2011

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc