Explore and see how internet-available works.

The following examples will show you how to use this library easily and within minutes in your project:

1. Quick usage

This module is made to be really easy to implement on your project. Just require the module and invoke the internetAvailable method. It returns a promise that is fulfilled if there's internet, otherwise if there's no internet it will be rejected:

var internetAvailable = require("internet-available");

// Most easy way
internetAvailable().then(() => {
    console.log("Internet available");
}).catch(() => {
    console.log("No internet");
});

And you have already checked for the status of the internet with Node.js !

2. Changing timeout and retries

If you want to make the verifier more dynamic, you can specify how many time can be tolerated during the execution of the script or set how many attempts should be made during this time. The method expects an optional object with at least the following settings:

var internetAvailable = require("internet-available");

// Set a timeout and a limit of attempts to check for connection
internetAvailable({
    timeout: 4000,
    retries: 10,
}).then(() => {
    console.log("Internet available");
}).catch(() => {
    console.log("No internet");
});

3. Custom DNS address to resolve

By default, internet-available resolves internally a web address to verify if there's internet or not namely google.com. If you aren't able to use this domain for verification or you just simply want to use a different one, you are able to change this settings during the initialization of the method:

var internetAvailable = require("internet-available");

// Make it with a different verification address
internetAvailable({
    domainName: "ourcodeworld.com",
    port: 53,
    host: '8.8.8.8'
}).then(() => {
    console.log("Internet available");
}).catch(() => {
    console.log("No internet");
});

As you can see you can change which port and host should be verified too (not required as you can only change the domainName).