6

All the solutions I've found simply poll a service. E.g. they ping google.com every second to see if the Node service has Internet access.

However, I'm looking for a cleaner event-based solution. In the browser, there's window.ononline and window.onoffline. I know these aren't perfect, but they're still better than nothing.

I'm not necessarily looking for a way to see if a Node service is online, I'm just looking for a way to see if the OS thinks it's online. E.g. if the OS isn't connected to any network interfaces, then it's definitely offline. However, if it's connected to a router, then I can maybe ping google.com.

Leo Jiang
  • 24,497
  • 49
  • 154
  • 284
  • There is nothing you can do without a loop, nodeJs is event based and it has a loop. so maybe just loop over pinging DNS instead of google to see that you are connected to a network, or rely on an other service that you talk to via a socket that will tell you when you are not connected to the internet or a specific network (probably a hardware based solution hard wired to your nodeJs running machine) – Kaki Master Of Time Dec 23 '19 at 22:30
  • Check this link, this might help https://stackoverflow.com/questions/15270902/check-for-internet-connectivity-in-nodejs – keedy Dec 24 '19 at 11:01

3 Answers3

1

I believe currently, that is the most effective way to tell if you're connected.

If you'd like to have an "event-based" solution, you can wrap the polling service with something like this:

connectivity-checker.js

const isOnline = require("is-online");

function ConnectivityChecker (callback, interval) {
    this.status = false;
    this.callback = callback;
    this.interval = this.interval;
    // Determines if the check should check on the next interval
    this.shouldCheckOnNextInterval = true;
}

ConnectivityChecker.prototype.init = function () {
    this.cleanUp();

    this.timer = setInterval(function () {
        if (this.shouldCheck) {
            isOnline().then(function (status) {
                if (this.status !== status) {
                    this.status = status;
                    this.callback(status);
                    this.shouldCheckOnNextInterval = true;
                }
            }).catch(err => {
                console.error(err);
                this.shouldCheckOnNextInterval = true;
            })

            // Disable 'shouldCheckOnNextInterval' if current check has not resolved within the interval time
            this.shouldCheckOnNextInterval = false;
        }
    }, this.interval);
}

ConnectivityChecker.prototype.cleanUp = function () {
    if (this.timer) clearInterval(this.timer);
}

export { ConnectivityChecker };

Then in your site of usage (E.g. app.js)

app.js

const { ConnectivityChecker } = require("/path/to/connectivity-checker.js");

const checker = new ConnectivityChecker(function(isOnline) {
    // Will be called ONLY IF isOnline changes from 'false' to 'true' or from 'true' to 'false'.
    // Will be not called anytime isOnline status remains the same from between each check
    // This simulates the event-based nature you're looking for


    if (isOnline) {
        // Do stuff if online
    } else  {
        // Do stuff if offline
    }
}, 5000);

// Where your app starts, call
checker.init();

// Where your app ends, call
// checker.cleanUp();

Hope this helps...

Kwame Opare Asiedu
  • 2,110
  • 11
  • 13
1

Your assumption is partly correct. From OS level, you can detect whether you are connected to a network or not, but that does not guarantee that you have access to internet. Also, checking natively whether I have network connectivity is going to be different for different OSs.

So, the most reliable way to understand whether you have Internet connectivity is to try accessing any resource from the internet, and see if you are successful. The resource might be non-accessible for some reasons, so your strategy should be accessing multiple well-known resources and see if you can access any of them.

For that, I have used the is-online npm package. It relies on accessing internet resources and DNS resolution to check whether you are connected to internet.

Example code:

const isOnline = require('is-online');

isOnline().then(online => {
  if(online){
     console.log("Connected to internet");
  }else{
     console.log("Not connected to internet");
  }
 });
WaughWaugh
  • 1,012
  • 10
  • 15
0

** UPDATE

I think this module has what you're looking for, it has many useful methods working with network interfaces.

https://www.npmjs.com/package/network

Basic example: tells you if you're connected to a gateway or not, and returns its IP.

*(i tested on my machin)

var network = require("network");

network.get_gateway_ip(function(err, ip) {
  if (err) {
    console.log("No Gateway IP found!");
  } else {
    console.log(`Gateway IP: ${ip}`);
  }
});
Tarreq
  • 1,282
  • 9
  • 17
  • 1
    The package literally just pings Google: https://github.com/ourcodeworld/internet-available/blob/master/internet-available.js#L28 – Leo Jiang Jan 05 '19 at 20:06