Check if the internet connection is up
Works in Node.js and the browser (with browserify/webpack).
In the browser you have navigator.onLine, but it's useless as it only tells you if there's a local connection, and not whether the internet is accessible.
$ npm install --save is-online
const isOnline = require('is-online');
isOnline().then(online => {
console.log(online);
//=> true
});Type: Object
Type: number
Default: 2000
Milliseconds to wait for a server to respond. This option is only supported in Node.js.
Type: string Array
Default: ['www.google.com', 'www.cloudflare.com', 'www.baidu.com', 'www.yandex.ru']
One or more hosts to check. Can either be a just a hostname, a hostname:port combination or a full URL like https://hostname.
In Node.js, we first contact one of the thirteen root servers and ask them to direct us to the servers which host the <root> zone (Which they are themselves). If the server answers, we return an online status.
If no satisfying answer is given within two seconds, we return an offline status. In the rare case where a firewall intercepts the packet and answers on its behalf, a second check is run which tries to connect to a series of popular web sites on port 80. If one of these connect, we return online, otherwise offline status.
In the browser, a sophisticated check like in Node.js is not possible because DNS and sockets are abstracted away. We use a check which requests an uncached favicon.ico on a series of popular websites. If one of these checks succeed, we return online status. If all the requests fail, we return offline status.
- is-online-cli - CLI for this module
- is-reachable - Check if servers are reachable
MIT © Sindre Sorhus