Network.js
A JavaScript library to measure various aspects of a connection. It can accurately estimate a bandwidth/latency between a client (using a web browser) and a server (running a specific script).
Installation
User Bower or download a ZIP file:
bower install --save network-js#2.0.0-alpha2
<script src="bower_components/network-js/dist/network.min.js"></script>
How to use
var settings = {
};
var net = new Network(settings);
net.latency.on('end', function(averageLatency, allLatencies) {
console.log('end', averageLatency, allLatencies);
});
net.latency.start();
net.upload
.on('start', function(dataSize) {
console.log('start', dataSize);
})
.on('progress', function(averageSpeed, instantSpeed) {
console.log('progress', averageSpeed, instantSpeed);
})
.on('restart', function(dataSize) {
console.log('restart', dataSize);
})
.on('end', function(averageSpeed, allInstantSpeeds) {
console.log('end', averageSpeed, allInstantSpeeds);
})
.start();
net.upload.abort();
net.download
.on('start', function(dataSize) {
console.log('start', dataSize);
})
.on('progress', function(averageSpeed, instantSpeed) {
console.log('progress', averageSpeed, instantSpeed);
})
.on('restart', function(dataSize) {
console.log('restart', dataSize);
})
.on('end', function(averageSpeed, allInstantSpeeds) {
console.log('end', averageSpeed, allInstantSpeeds);
})
.start();
net.download.abort();
Settings
The available settings with their default values:
{
latency: {
endpoint: './network.php',
measures: 5,
attempts: 3
},
upload: {
endpoint: './network.php',
delay: 8000,
data: {
size: 2 * 1024 * 1024,
multiplier: 2
}
},
download: {
endpoint: './network.php',
delay: 8000,
data: {
size: 10 * 1024 * 1024,
multiplier: 2
}
}
}
Here is an example usage:
var net = new Network({
endpoint: './my-new-endpoint/',
download: {
data: {
multiplier: 2.5
}
}
});
You can also redefine settings whenever you want:
net.settings({
endpoint: './my-second-new-endpoint'
});
console.log(net.settings());
net.latency.settings({
measures: 10
});
console.log(net.latency.settings());
Compatibility
Network.js is based on two browser features: Resource Timing and XMLHttpRequest (v2). While the first one can be polyfilled, the second one is a requirement.
Thus, Network.js should be compatible with:
Browser | Partial support (polyfill) | Native support |
---|
IE 10+ | | ✔ |
Firefox 35+ | | ✔ |
Chrome 29+ | | ✔ |
Opera 15+ | | ✔ |
Android Browser 4.4+ | | ✔ |
| | |
Safari 5+ | ✔ | |
iOS Safari 5.1+ | ✔ | |
Firefox 12+ | ✔ | |
Opera 12.1+ | ✔ | |
Android Browser 3+ | ✔ | |
Latency measures can be very far from reality if the browser doesn't have native support and uses the provided polyfill.
Caveats
- Latency measures never return any results with Firefox.
- Chrome cannot upload a ~128 MB file, which will mainly affect fiber users.
- Currently, the client and the server must be on the same domain or measures can't be done due to the same-origin policy.
Compilation
To compile the project, install the latest version of Node and run these commands inside a terminal:
git clone https://github.com/nesk/network.js.git
cd network.js
npm install
npm run build
There's also a watch
script which compiles the project whenever a file is changed:
npm run watch
To check if the project passes all the tests, run:
npm test
Contribution
Read the CONTRIBUTING file.
License
This project is licensed under the MIT license, check TLDRLegal for details.