Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9217621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:40:24+00:00 2026-06-18T02:40:24+00:00

I’m new to Backbone and I’m trying to build a Backbone app that graphs

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:40:25+00:00Added an answer on June 18, 2026 at 2:40 am

    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 });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.