Check out my books on Amazon at https://www.amazon.com/John-Au-Yeung/e/B08FT5NT62
Subscribe to my email list now at http://jauyeung.net/subscribe/
With the advent of mobile devices like phones and tablets, knowing the connection status is very important since it can change any time, affecting user experience in the process. We also have to be aware of different kinds of internet connections since they vary widely in speed.
Fortunately, we have the Network Information API built into browsers to check for internet connection status.
This API is available for browser and worker contexts.
In this article, we’ll look at how to use the API to get network connection type changes and connection status.
Detect Connection Changes
Detecting connection changes is simple. We can use the navigation.connection
object to listen to network type changes as follows:
const connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;
let type = connection.effectiveType;
const updateConnectionStatus = () => {
console.log(`Connection type changed from ${type} to ${connection.effectiveType}`);
type = connection.effectiveType;
}
connection.addEventListener('change', updateConnectionStatus);
We can then test the connection type changes by going to the Chrome developer console by pressing F12. Then go to the Network tab, and then on the top right, there’s a dropdown to change the connection type.
If we switch between them, we should see something like:
Connection type changed from 4g to 3g
Connection type changed from 3g to 4g
Connection type changed from 4g to 2g
from the console.log
output.
The connection
object is a NetworkInformation
object instance and it has the following properties:
-
downlink
— effective bandwidth estimate in megabits per second rounded to the nearest 25 kbps. -
downlinkMax
— maximum downlink speed, in Mbps for the underlying connection technology -
effectiveType
— the type of connection determined by a combination of recently observed round-trip time and downlink values. It can be one of ‘slow-2g’, ‘2g’, ‘3g’, or ‘4g’. -
rtt
— estimated effective round-trip time of the current connection rounded to the nearest multiple of 25 milliseconds. -
saveData
— boolean indicating if reduced data usage optional has been set -
type
— type of connection to communicate with the network. It can be one ofbluetooth
,cellular
,ethernet
,none
,wifi
,wimax
,other
,unknown
Compatibility
This API is new and experimental, so we’ve to be careful when we use it. Chrome supports most properties out of the box since Chrome 61. Some options like downlinkMax
and type
are only available for Chrome OS. Firefox and Edge do not support this API.
It’s also available for use in other Chromium-based browsers like Opera, Android Webview, and Chrome for Android.
With the Network Information API, we can get information about the network connection. This is useful for detecting the connection type of mobile devices and lets us change the web page to accommodate slower connections accordingly.