Understanding how backbone.sync works is very useful. API servers are never 100% perfect.
Backbone.sync is global and other calls are just proxies to it.
methodcan be any of CRUD, i.e
UPDATEand these are called by i.e. fetch with
optionshave the i.e.
Sync is called usually from
create(). Sync is a proxy to an
$.ajax request that attaches sucess and error callbacks to that
When overriding, you don't need to override the global sync. Yould override sync on a particular object
It's good practise to save a reference to the original
Backbone.sync in case you want to reach out and use the old
sync, say if you wanted to rely on it as a fallback.
Usually methods that interface with sync, take your success callback and doing something in the middle and then call
HTTP has a number of drawbacks:
- it's unidirectional, you have to ask for the content
- it's Half-duplex :(
- Costly, there are loads of headers!
- Polling: high latency.
- Long polling leaves a request hanging, which can time out, causing too many requests. Websockets!
- full duplex
- bidirectional - client or server can send, you can just subscribe to a channel, and go!
- Include socket.io from your server:
This shouldn't be included on the client, so that the server can choose what to send you: 2. Initialize a connection on your app namespace on your clientside:
app.socket = io.connect('ws://yourserver:4000')
Integrating into Backbone.
- Listen to events and update existing
CollectionsSocket.io can be used as an event buffer, stretched out accross a load of clients!
You can use socket.io for persistence, as long as you set it up for persistence. Respond with a callback to the 'create' event, and provide handlers for CRUD events.
Always check for the first argument to socket.io emit's
done callback to see if it is an error!
The way in which we patched sync means that we can just add a
realtime = true flag to a model or collection
We can push from the serer to the client by adding a listener to a
new event. The listener might be on a high level entity such as the
router. You can initialize it in the router, put it in the
main.js or the
app object, i.e. somewhere high level in your app. The initialising of a collection is a good place to bind it to a socket!