Is there a method in Applab to check internet connection?

One of my student is working on a cloud save mechanism, and he found a bug that will wipe existing data if a user creates a profile (with a same name as an online save) offline and sync it online, forcing a rewrite of wrong data (the create part would be prevented if the user is online, when the code goes through the entire cloud save list (retrieved out of the internet), looking for matching names). He’s wondering if there’s a method to return the status of internet connection, like navigator.online, in Applab, so he can disable the create part if the user is currently offline.

Hi @i15904503668i,

If it’s not something you can find in the toolbox, unfortunately it’s unlikely to be supported. However, I’ll forward the question to code.org staff to get the official word.

I haven’t used the internet features of AppLab, but I’m wondering if you might be able to use something like “startWebRequest” (in the Data toolbox) to make a request to a known server, and if it returns an error like 500, then you’d interpret that to be no internet connection.

Frank

1 Like

Hey,
I forwarded this to my student and guess what, your suggestion worked perfectly. My student want me to say thank you very much.
For anyone who is looking for a way to detect internet connections: Use startWebRequest() with a certified web address from appLab Documentation and a function with a condition named status. Then check if status equals 200 (it means everything is okay, connection is established), like this:

startWebRequest(“https://www.accuweather.com”, function(status){
if (status !== 200){
hideElement(“getUserIDButton”);
}});
Edit: After trying this method on my own I have to say it’s buggy and laggy - it takes time for the request to complete (and the code won’t stop because of it), and some sites, even when they’re working, will return an error status. It would REALLY be better if something like navigator.isOnline is in place.

It seems that all approved addresses returns status 400 - bad request. startWebRequest could be broken.
Furthermore, lots of the approved websites no longer exists, like googleapis.com and api.thingspeak.com
Could you please forward this concern to code.org staff too?

Hi @i15904503668i - we’ve combed through the approved websites and cleaned up the documentation. Thanks for bringing this to our attention!