I'm new to Backbone and I'm trying to build a Backbone app that graphs and maps data in real-time. I implemented a websocket following this example code. The problem is, I would like to use a more extensive data set than the example code, and if I understand the code, it is just creating one model -- a single array of points. I want a collection of models in which each model has latitude, longitude, and amount (just a numerical value).
How do I implement a websocket such that when my backend sends some JSON, my app creates a new model with those attributes? I've read on blogs about this that I need to override Backbone.sync and implement an event aggregator, but the only examples I've seen of this use socket.io. Socket.io is not an option because of the language/framework I'm using on backend. Moreover, eventually I'll be switching out the backend to another language that also isn't supported by socket.io, so I'd like to find a more general way to implement the websocket on the frontend that does not involve a library like socket.io.
You can use Backbone.WS which lets you use Backbone resources over native WebSockets. No extra dependencies!
I have found a solution to my own question that works. Again, I'm new to Backbone, so I'm not sure if this is the best way -- would be interested in feedback on whether this solution is following best practices. The code is based on this example by Andrew Cholakian. I kept some print statements in that are helpful when you run the code.
The code assumes your backend is sending JSON data in the form of
{data: "{"lat": latitude, "long": longitude, "amt": amount}"}
You can use backbone.wamp, based on WAMP protocol over WebSocket