Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Re-using Backbone.js models on the server side with Node.js and Websockets

I've been working my way through:

http://blog.andyet.com/2011/02/15/re-using-backbonejs-models-on-the-server-with-node

I have a few questions about sharing models server-side and a few questions about overriding sync. Real-time model syncing architecture ftw.

  1. Models
    So in this example he syncs his whole application state. Part of my application state is the User model, this handles things like logging in, finding the type of platform they are using, etc. Am I using this wrong? I have client side session data in this model, something that really doesn't need to be on the server, should I put this on the server anyway?

    For other models that are strictly application data that should be synced with the server, how do I manage these model on the server? Is there a "view" type component that handels changes to the model and acts on the model how the server needs to?

  2. Sync
    I want to override the sync method to sync with the server and any other clients that need the updated data. How could you write a sync method that works client -> server and server -> client so that no matter where it is called everyone get updated?

like image 845
fancy Avatar asked Sep 18 '11 23:09

fancy


People also ask

Do people still use Backbone JS?

Backbone. Backbone has been around for a long time, but it's still under steady and regular development. It's a good choice if you want a flexible JavaScript framework with a simple model for representing data and getting it into views.

Can JavaScript work on server side?

Server-Side JavaScript vs Client-Side JavaScript So JavaScript can now be used not only on the client-side but also on the server.

What is the use of backbone JS?

BackboneJS is a lightweight JavaScript library that allows to develop and structure the client side applications that run in a web browser. It offers MVC framework which abstracts data into models, DOM into views and bind these two using events.

Is backbone JavaScript framework?

Backbone. js is a JavaScript framework that helps you organize your code. It is literally a backbone upon which you build your application. It doesn't provide widgets (like jQuery UI or Dojo).


1 Answers

There are a a couple of aspects that make backbone a very good fit for client applications, but not not at all useful for server-based environments.

The core of backbone is its Events module, which the framework is built around (models are basically event-managed collections, views are glue code for rendering based on model event changes, etc...), is pretty much useless on a server: the only real event you're ever getting is the request, or the various events for socket data, and both are handled by (and are taken care of for) by the middleware and node itself.

Models:

  • if you're using some kind of ORM on your server, it should already provide the event handling necessary for dealing with model changes. And since you dont do any dynamic view updates on the server, you don't need any of the infrastructure backbone provides for models.

  • if you aren't using an ORM (as in realtime, never logged chat :), you can use Backbone's models, but they aren't suited for larger datasets or any kind of storage, and in the end you're still using an underscore-wrapped Hash / Array.

like image 189
galileoMonkey Avatar answered Sep 18 '22 01:09

galileoMonkey