3

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.

Awshaf Ishtiaque
  • 441
  • 5
  • 11
  • Check at ookla.com to see if they have a library you can use. They make the popular speedtest.net service. – Barmar May 06 '21 at 18:51
  • 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". The linked answer shows how to do this manually, but there are a slew of online services a Google search away that you can use instead. – Abion47 May 06 '21 at 18:54
  • As far as best practices go, AFAIK there is no best practice for doing this in a way that it will populate a realtime graph. The reason being that having a realtime graph of download/upload speed is a bad idea, since the only way to populate that graph is to be constantly downloading and uploading stuff which is going to wreak havoc on your user's bandwidth and burn through their internet usage caps (if applicable). – Abion47 May 06 '21 at 18:57
  • Alternatively to my first comment, speed tests can also go "download/upload an arbitrary amount of data over a fixed time frame, then calculate per-second speeds based on how much data was successfully transferred over that time". A slightly different approach, but the fundamental principle is the same. – Abion47 May 06 '21 at 19:01
  • So basically we just try to test the speed with how fast the api is responding right? I'm looking for a good api with node.js compatibility – Awshaf Ishtiaque May 06 '21 at 20:54

1 Answers1

0

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);
    });
}
Awshaf Ishtiaque
  • 441
  • 5
  • 11