Skip to content

Instantly share code, notes, and snippets.

@6ewis
Forked from mjm918/SpeedTest.js
Created April 5, 2022 22:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 6ewis/4584f5456660d84a688e72b2cec32cbf to your computer and use it in GitHub Desktop.
Save 6ewis/4584f5456660d84a688e72b2cec32cbf to your computer and use it in GitHub Desktop.
React native check internet speed
export const fetchParallel = (link,header, callback) =>{
const POOR = 150;
const MODERATE = 550;
const GOOD = 2000;
const _start = new Date().getTime();
const speedTester = RNFetchBlob
.config({
fileCache : true
})
.fetch('GET', SPEED_TESTER_FILE, { }); // SPEED_TESTER_FILE some file address. try to download a file size of > 700kb
const execution = Promise.all([
fetch(link,header),
speedTester
]);
execution.then(([req, speed]) => {
return Promise.all([req.json(), speed.path()])
})
.then(([req, res]) => {
callback(req);
const _end = new Date().getTime();
const kbPerSecond = Math.floor(1024/((_end-_start)/1000));
var slow = false;
var good = false;
if(kbPerSecond <= POOR){
// very bad connection
Warning('very bad connection');
slow = true;
}else if(kbPerSecond >= POOR && kbPerSecond <= MODERATE){
// avg speed
Warning('avg speed');
slow = true;
}else if(kbPerSecond >= MODERATE && kbPerSecond <= GOOD){
// good connection
Warning('good connection');
good = true;
}else if(kbPerSecond > GOOD){
// excellent connection
Warning('excellent connection');
good = true;
}else{
// unknown type
Warning('unknown type');
slow = true;
}
RNFetchBlob.fs.unlink(res);
if(slow){
}
if(good){
}
}).catch(error=>{
// alert error
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment