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.
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}"}
// this function opens the websocket and will trigger add_point when
// a new message is received
Stream = function () {
_.extend(this, Backbone.Events);
var self = this;
self.socket = new WebSocket("ws://" + document.domain + ":5000/websocket");
console.log("Using a standard websocket");
self.socket.onopen = function(e) {
self.trigger('open', e);
console.log('socket opened');
};
self.socket.onerror = function(e) {
self.trigger('error', e);
};
self.socket.onmessage = function(e) {
self.trigger('message', e);
self.trigger('data', e.data);
self.trigger('add_point', JSON.parse(e.data));
};
self.socket.onclose = function(e) {
self.trigger('close', e);
console.log('socket closed');
};
};
DataPoint = Backbone.Model.extend({
defaults: {
lat: null,
long: null,
amt: null
}
});
DataSet = Backbone.Collection.extend({
model: DataPoint,
initialize: function(options) {
this.stream = options.stream;
var self = this;
this.stream.on("add_point", function(pt) {
self.add( new DataPoint({
lat: pt.lat,
long: pt.long,
amt: pt.amt
}));
console.log('updated collection');
console.log(self.models);
});
}
});
MapView = Backbone.View.extend({
initialize: function(options) {
this.dataSet = options.dataSet;
}
});
$(function() {
var stream = new Stream();
var dataSet = new DataSet({ stream: stream });
var mapView = new MapView({ dataSet: dataSet });
});
You can use Backbone.WS which lets you use Backbone resources over native WebSockets. No extra dependencies!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With