29

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?

Sangharsh
  • 2,999
  • 2
  • 15
  • 27
fancy
  • 48,619
  • 62
  • 153
  • 231
  • 4
    I would recommend against using backbone on the server. It's a nice idea but it just does not work. It's an abstraction that leaks and get's in the way – Raynos Sep 18 '11 at 23:45
  • @Raynos Can you give me some more info why you say this, and how you would go about implementing it? – fancy Sep 19 '11 at 00:24
  • http://fzysqr.com/2011/02/28/nodechat-js-using-node-js-backbone-js-socket-io-and-redis-to-make-a-real-time-chat-app/ makes it look good too. – fancy Sep 19 '11 at 00:24
  • 1
    personal experience trying to use backbone says it get's in the way. Real projects say that it's not a good tool to use. It's developed for the client, there are better patterns for the server. – Raynos Sep 19 '11 at 00:27
  • 1
    @Raynos thanks, any chance you could point me toward some resources explaining a pattern that would work well service-side in this situation? Should I go about it a similar way (mvc) but just build it from scratch? – fancy Sep 19 '11 at 00:30
  • I would recommend an EDA driven pattern. Look at the [hook.io](https://github.com/hookio) examples or look at [ExpressArchTest](https://github.com/Raynos/ExpressArchTest). If I had a good architecture example I was pleased with I would share it ;) – Raynos Sep 19 '11 at 00:38
  • I've also recently updated [this](http://stackoverflow.com/questions/6241934) and [this](http://stackoverflow.com/questions/5683916) to reflect those libraries are overkill. If you want some high level help you can always try [node.js chat](http://chat.stackoverflow.com/rooms/642) – Raynos Sep 19 '11 at 00:40
  • 2
    @Raynos I could see how backbone would feel redundant if your webapp was talking directly to the datastore. Where backbone on the server has really worked for us is when the webapp is talking to one or more REST APIs as datastores. So, if you have an app that's talking to a REST api, and there's not already client libraries for it, Backbone works really well as a quick and useful form of generic client library. – Edward M Smith Sep 19 '11 at 21:42
  • @EdwardMSmith We were going to use it in this capacity, https://github.com/andyet/capsule but it was just one of the options we were looking into. Do you use it this way at all? If so any feedback or advice would be great. Thanks! – fancy Sep 19 '11 at 22:56
  • Backbone does seem a bit overkill. I can't speak to 1) without knowing more about your data, but I'd recommend keeping it simple. And 2) Check out nowjs as a way of syncing client and server-side data. – nicholas Oct 03 '11 at 19:00

1 Answers1

4

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.

galileoMonkey
  • 715
  • 6
  • 12
  • 1
    Server-side models would still allow for validations, which is perhaps the biggest reason to reuse models server side. – Mario Oct 01 '12 at 16:48
  • Yes, you can use for server-side validation, but what you REALLY want is to reuse the validation and schema DECLARATIONS, because most of the schema & validation already comes with the ORM package, and is usually more robust then Backbone's own models (which are still wrappers around Object and Array respectively). – galileoMonkey Oct 05 '12 at 13:42