During the development of Pahvi we needed to add model synchronization to Backbone.js.

We decided to give ShareJS a try. It’s developed by Joseph Gentle who worked on Google Wave. It’s an awesome library that allows developers to build small Etherpad clones with just few lines of JavaScript code. ShareJS has a great support for JSON documents which is pretty nice fit for Backbone Models.

Backbone.SharedCollection is collection that listens to all changes events of the models within the collection and saves the changes to a ShareJS document. It also listens events coming from ShareJS and reflects them to models. Neat thing about this is that no special code is required for the models. They just automatically get synced between all browser instances when they are added to the collection.

This commit shows the few required changes to make the TODOs example app in Backbone.js collaborative. Not too bad. The full example is in the Github repository.

ShareJS also supports Redis as persistence option. So this also gives persistence for Backbone Models for free which is great when building prototypes. Just build client only prototype with Backbone and when you need synchronization and persistence you can just drop Backbone.SharedCollection in and you are set.

For more information and API docs checkout README.md in Github.

Future

ShareJS supports text operatios within string fields in JSON documents. It would be cool to build Backbone Model where a field could act as storage for mini etherpads.

In Pahvi we have editable Text Boxes and currently the text and text style attributes are stored as attributes in a Backbone Model. For style attributes this works great since it’s ok to allow latest update to override previous, but for text this is problematic. Users will lose data when editing the same Text Box concurrently. It would be ideal if we could keep this Model abstraction over the Text Box data and allow Etherpad style editing to it.

Ideas and comments about this would be very welcome.

Rich text support is also something were are hoping to see in ShareJS soon 🙂

Esa-Matti Suuronen