Please note, this is a STATIC archive of website developer.mozilla.org from November 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Rec Room - Networking

Draft
This page is not complete.

XHR and calling Web Services

System XHR

In order to communicate with a web service, we can specify a `systemXHR` permission in our application manifest. In our podcasts app, we have this permission set to allow us to fetch and download podcasts. In our episode model, app/scripts/models/episode_model.js, we can see how to put XHR to use:

var request = new XMLHttpRequest({mozSystem: true});
request.open('GET', this.get('audioURL'), true);
request.responseType = 'moz-chunked-arraybuffer';
request.addEventListener('load', function() { 
  console.log(request.readyState, request); 
  if (request.readyState === 4) { 
    _this._loadComplete = true;
  }
}); 
request.addEventListener('progress', function(event) { 
  console.info('eventProgress', _this.get('_chunkCount')); 
  localforage.setItem('ep:' + _this.get('id') + _this.get('_chunkCount'), request.response)
    .then(function() { 
      // Increment our internal data chunk count.
      _this.incrementProperty('_chunkCountSaved');
      if (_this.get('_loadComplete')) { 
        console.log('chunk data assembled');
        _this.set('isDownloading', false);
        _this.set('isDownloaded', true); 
      } 
    }); 
    _this.incrementProperty('_chunkCount'); 
  });
 request.addEventListener('error', function(event) { 
  window.alert('Error downloading this episode. Please try again.'); 
 });
request.send(null);

Web Sockets

The web sockets API allows you to send messages to a server and receive event-driven responses without having to consistently poll for a reply. Though it is still under active development, it's a promising technology that makes it easy for you to maintain a connection stream between client and server.

Previously, all HTTP communication was handled by the client, which required persistent polling of the server for new data. With web sockets, both the server and client have the ability to send data at any time. This is especially useful when your application needs real-time data updates.

More information on working with Web Sockets is available on MDN here.

Document Tags and Contributors

 Contributors to this page: brittanystoroz
 Last updated by: brittanystoroz,