July 24th, 2013, 02:58 PM
Backbone.js & Websockets
I'm trying to get my head around how the use of the web socket protocol will result in a view in a web app being kept in sync with records/documents in a collection/table.
The basic architecture is:
Backbone.js View (client) --> Backbone.js Model (client) --> REST service (server) --> Business Layer (server) --> DAO Layer (server) --> Database
The idea is to create a model in Backbone.js from which Backbone generates the view & then to use websockets by overriding Backbone.sync method to enable real time updates from changes in the DB.
What I'm fundamentally not understanding is that, after I have a webpage open on the client displaying a view of a Backbone model, if I make a change to add a record/document to the collection/table in the database, how does the architecture propagate that change in the DB back up the layers to the REST service & back (pushed) onto the client (Backbone model)?
July 24th, 2013, 06:19 PM
You'd probably want use something akin to the observer pattern.
When you create a model on the client-side, you would want to send some message over your open socket indicating that you want to listen for changes to that particular data. In either your business layer or your DAO layer, you would notify any observers whenever a data change is made. When your server-side code is notified of a data change, it can send some message back over the open socket to the client-side and the client-side can update its model.