Network Stats

This article is in need of a technical review.

This article is in need of an editorial review.

DRAFT
This page is not complete.

Non-standard

This API is available on Firefox OS for certified applications only.

Summary

The Network Stats API allows to monitors data usage and exposes this data to privileged applications.

Data can be access through the navigator.mozNetworkStats which is an instance of the MozNetworkStatsManager interface.

Accessing data

Information about the volume of data received and send are automatically stored by the system. Accessing them is possible by using the MozNetworkStatsManager.getNetworkStats() method. This method expect a configuration object as its first parameter, which must contain the following properties:

  • start: A Date object representing the beginning of data measurement.
  • end: A Date object representing the end of data measurement.
  • connectionType: The origin of the data. It can be wifi, mobile or null. If null, data measurement from both origin are merged. To know in advance which kind of origin is available, the MozNetworkStatsManager.connectionTypes property return an Array of string representing each supported origin.

When called, this method return a DOMRequest to handle the success or failure of the information request. In case of success the request's result is a MozNetworkStats object.

var manageWifi   = navigator.mozNetworkStats.connectionTypes.indexOf('wifi')   > -1;
var manageMobile = navigator.mozNetworkStats.connectionTypes.indexOf('mobile') > -1;

var config = {
  start: new Date(),
  end  : new Date(),
  connectionType: manageWifi ? 'wifi' : null
};

var request = navigator.mozNetworkStats.getNetworkStats(config);

request.onsuccess = function () {
  console.log("Data received: " + request.result.data[0].rxBytes + " bytes");
  console.log("Data sent: " + request.result.data[0].txBytes + " bytes")
}

request.onerror = function () {
  console.log("Something goes wrong: ", request.error);
}

Sampling over time

To get a vision of the data usage over time, the information about the amount of data is stored in chunk, each chunk is a value representing the amount of data exchange since the last chunk was stored.

When requesting for the stats, the resulting MozNetworkStats object contain as much data chunk as possible for the interval define between the start and end date. The total number of chunks depend on two parameters (note that those parameters are read only):

Each data chunk is a MozNetworkStatsData object, and all the data chunks for a given time frame are available through the MozNetworkStats.data property, which is an Array of MozNetworkStatsData objects.

var rate = navigator.mozNetworkStats.sampleRate;
var max  = navigator.mozNetworkStats.maxStorageSample;

var config = {
  start: new Date() - (rate * max), // This allows to get all the available data chunks.
  end  : new Date(),
  connectionType: 'mobile'
};

var request = navigator.mozNetworkStats.getNetworkStats(config);

request.onsuccess = function () {
  var total = {
    receive: 0,
    send   : 0
  };

  this.result.forEach(function (chunk) {
    total.receive += chunk.rxBytes;
    total.send    += chunk.txBytes;
  });

  console.log("Since: " + config.start.toString());
  console.log("Data received: " + (total.receive * 1000).toFixed(2) + "Ko");
  console.log("Data sent: " + (total.send * 1000).toFixed(2) + "Ko")
}

request.onerror = function () {
  console.log("Something goes wrong: ", request.error);
}

Specification

Not part of any specification.

See also

Tags (5)