How to use backbone.js with websockets/socket-io/nowjs - javascript

I am just getting into backbone.js and am finding progress a little slow. My main problem is working out how to keep my client and server side models in sync using socket-io (technically I am using now.js but the same principal should apply).
I think the best way is to override the sync method but some simple advice would be really welcome.

Simply overwrite Backbone.sync so that it sends messages down socket.io and tells the relevant backbonejs models on the server to alter state.
The interesting part of this solution is setting up the master-master relationship. You need to insure that for any client they can only "update" the state of models on the server that they have "ownership" of to avoid hackers and server-side state corruption.
So for each client they have a set M where that client is the master of all models in M and has a set S where that client has slaves of all the models in S.
It can only force updating on the server of models in M and only one client should have a particular model in M (or you need to implement a solid locking / merging implementation).
Whenever a model on the server is updated you simply push out to any client who has that model in S. (and push to any client who has that model in M if the model is in M for multiple clients).
A lot of thought needs to go into control / permissions and ownership that is normally handled by the MVC controller once a client POST/PUT/DELETE some data.

Check out backbone.iobind: https://github.com/noveogroup/backbone.iobind
It overrides Backbone.sync for you.

A much better approach is event-driven architecture using an event aggregator. Great read on the subject is the following Derick Bailey's article => Decoupling Backbone Apps From WebSockets
It keeps stuff highly decoupled, enables easier testing and changing websockets lib, and on top of it all, it doesn't mess up with overriding Backbone's internals like sync()

Maybe this excellent tuto will help you:
https://blog.andyet.com/2011/02/15/re-using-backbonejs-models-on-the-server-with-node

Related

Java solution for meteor DDP server

I have a web application that reflects the content of a list from my server. To do that I use websockets (socket.io) to listen to update messages from my server.
After having a good first snapshot of the list, it receives update events like {'action':'changed','type': 'typeA', 'id':1}, then the page can make a request to http://server.com/api/typeA/1 and insert, delete or replace the updated item in the model.
The problem is, if any update event occur while my websocket connection is being stablished the system will lose those and be delayed. Or if it requests the first snapshot after the connection event happens, the request may complete after some update is signaled and then the new value may be replaced by an ancient one.
Is there some lib to make what Meteor DDP does for publishing a generic DB in a server written in java?
We came across many distributed data mechanisms and ended up choosing a data sync strategy using deepstream.io that implements the features that we wanted for cloning a collection from the server by sending updates on demand, and have a good and well supported framework for JS and Java.
It worths giving it a try.
Please take a look over: https://github.com/Reactive-Extensions/RxJS
I think this is what you're looking for.
Thank you,
Alex S.

Model code duplication when using Ember.js

I'm considering switching to Ember.js for one of my Sinatra applications. Our current setup is using server-side Handlebars rendering along with RESTful routes for CRUD operation and Websockets for push notifications.
My main concern, however, is duplication of model code.
Ember Data is a library for loading models from a persistence layer (such as a JSON API), updating those models, then saving the changes. It provides many of the facilities you'd find in server-side ORMs like ActiveRecord, but is designed specifically for the unique environment of JavaScript in the browser.
From what I understand, to use Ember.js, I need to define my models in Javascript, thus duplicating a lot of the Mongo models that we already have server-side. Is this correct? How is it possible to mitigate code duplication in models when using Ember.js in combination with a Ruby backend?
You will probably want to split up some of the tasks between being done on the client side and being done server side.
The models themselves will be duplicated since it makes it easier to work with Ember if you have Ember Data models. Though, you could build your own models on the fly and just have api calls for everything (probably not advised). You would do that by doing the api calls within the model hook of the route.
Once you have Ember Data models setup though, you could keep your logic fairly simple and have the server to more complicated tasks. But this largely depends on application specific needs.

End to end MVVM using knockout (javascript)

I am new to MVVM , and i wanted to understand ,
if you have a model in the back end, say a c# library which is getting data from the database or any other service. how would the Model notify the viewmodel.
i understand in the MVVM INotifyprovier does that for WPF (just read it somewhere), but how about Web app based scenario, does Viewmodel in java-script always have to ping Model to identify if there is a change in the model and then propagate to UI.
I am assuming that the viewmodel would always have to send Ajax request to a asmx or api with a set interval of 10 minutes or so (just an example).
is that how it works end to end. any example will be great.
Because one of the properties is, automatic UI refresh, so I am assuming that fresh data needs to be asked from server at regular intervals. A lot of examples i see on the web is only interacting between ViewModel and View, but hardly i see any thing with Model, ViewModel and View all combined together.
I could think that if one uses SignalR which sends a ping from Server to the client, then the Viewmodel could be updated and hence the View.
But if every time you need to ask a fresh set of data from Sever, so what's with the hype of Knockout, Jquery has been doing that for a while, except the Declarative binding stuff, in the knockout library.
Appreciate if somebody could correct me.
Thanks
It is up to your client application to fetch new data since server doesn't have any concept of observables. There is also a useful mapping plugin that could automatically map your javascript data from the server into an observable. That way when you fetch data you don't have to re-map your data.
However, it is possible to notify client of changes from server. One way is to make server push changes to the client is via HTML5 WebSockets. SignalR is a good library candidate for that task. It that would open a WebSockets connections so server can notify client of changes. And you would use Service Broker SqlDependency to trigger event notifications on updates. An example could be found here.
Good luck!
Also here is a really good video to watch about knockoutjs that would give you understanding of the framework.

Recommendations regarding sharing model code in node.js and browser

I am using Sequelize as my server side ORM. Is there a recommended approach towards sharing the Model code (especially the validations) with my client application ?
Please don't recommend solutions which require me to move to a NoSQL database. Currently that is not an option for me. While I really Sequelize as an ORM, I am willing to move onto some other model implementation if it is beneficial.
Persistencejs, but it doesn't appear to be as well maintained as Sequelize:
https://github.com/zefhemel/persistencejs
At present there does not seem to be an end to end solution ie. a model library which you could just require in either browser or server and define your models - where methods like save, update would be polymorphic requiring the developer to just extend standard model classes and use them on either client or server.
However, for people looking for a similar solution - I recommend using JSON schema validators, which are quite hassle free and provide a simple means to share your validation logic between client and server.

Using MVC in Javascript without any server-side language?

I'm in the process of trying to wrap my head around using MVC and using it with javascript. I'll be making a mobile app with HTML5 and don't plan to use any server side languages only local storage.
How can this be achieved?
I'm usually thrown off by the model since it relies on the back end language from my understanding.
The Model doesn't rely on the back end. MVC in conceptually simple:
Controller gets request.
Controller calls for Model to do all of the logic (be it database interaction, localStorage interaction of whatever)
Model returns data to Controller.
Controller calls View accordingly, and if needed, passes data from the Model.
This means, that Model doesn't have to use a server side language, it's a placeholder for the application logic.
There are multiple MV* frameworks available. You don't have to have server support to use them.
BackboneJS
EmberJS
javascriptMVC
There are many more, but these three come to mind first. Enjoy
MVC development has nothing to do with server side scripting.
It only relate to the type of architecture you are taking to develop your application.
It only means you have Models, Views and Controllers. Models can be object generated from javascript which are used with the Views in order to render some data.
Controllers are there to hold the logic and behaviour between the view and the models.
Well that's a very basic overview, but by no means does MVC means backend services or ajax calls.
This article might help http://www.alexatnet.com/articles/model-view-controller-mvc-javascript

Categories