How to measure real time internet speed using Javascript?

Viewed 1914

I am trying to figure out how to detect internet download and upload speed using JavaScript.

The other solution posted in the other stackoverflow thread using a dummy download file has some cons ( which can be found in its comment section ) and is not suitable for production level code.

What is the best practice to detect the internet download and upload speed using node.js? Using a library is okay. I want to use this value to plot a real time graph like the one found in Task Manager>Performance>"Wi-fi" graph.

1 Answers

As @Abion47 said, "Every download/upload test is going to be some variant of "download/upload some dummy file of known size and time how long it takes to transfer".

Therefore I worked around this problem using fast-speed-test-api. The API did not include much documentation so I included my working example below:

const FastSpeedtest = require("fast-speedtest-api");
   
const myPromise = fastnetApi();
myPromise.then(res =>{
  console.log('it returned:', res);
  return res 
})

function fastnetApi() {

    let speedtest = new FastSpeedtest({
        token: "YOUR-TOKEN(CAN BE FOUND IN THE GITHUB DOC)", //**** required
        verbose: false, // default: false
        timeout: 5000, // default: 5000
        https: true, // default: true
        urlCount: 5, // default: 5 
        bufferSize: 8, // default: 8 
        unit: FastSpeedtest.UNITS.Mbps // default: Bps
    });

    return speedtest.getSpeed().then(s => {
        console.log(`Speed: ${s} Mbps`);
    console.log(typeof s)
    return s;
    }).catch(e => {
        console.error(e.message);
    });
}
Related