Events for network status change in Node.js?

All the solutions I've found simply poll a service. E.g. they ping 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

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:


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.timer = setInterval(function () {
        if (this.shouldCheck) {
            isOnline().then(function (status) {
                if (this.status !== status) {
                    this.status = status;
                    this.shouldCheckOnNextInterval = true;
            }).catch(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)


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

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

Hope this helps...

Online/Offline Event Detection, Online and offline event detection can be implemented in the renderer process using Therefore, if you really want to determine the internet access status of Example: main.js const { app, BrowserWindow } = require('electron') let const updateOnlineStatus = () => { ipcRenderer.send('online-status-changed', navigator. The MongoDB Node.js driver will automatically attempt to reestablish connections in the event of transient network errors or elections. In those cases, the driver will use its cached copy of the most recent resume token so that no change stream events are lost.

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 => {
     console.log("Connected to internet");
     console.log("Not connected to internet");

Net, The net module provides an asynchronous network API for creating For example, it is passed to the listeners of a 'connection' event emitted on a net.​Server  Node.js has a built-in module, called "Events", where you can create-, fire-, and listen for- your own events. To include the built-in Events module use the require() method. In addition, all event properties and methods are an instance of an EventEmitter object.


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

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}`);

Online and offline events, Some browsers implement Online/Offline events from the WHATWG Web the OS reports a change in network connectivity on Windows, Linux, and OS X. Network status autodetection was implemented in Firefox 2. or .onoffline properties on document or document.body to a JavaScript Function object. The latest hot topic is node.js. Some of us went to Full Frontal Javascript Conference and were very excited by the power and potential of node.js which Simon Willison (Django core team) introduced there. Simon describes node.js as “A toolkit for writing extremely high performance non-blocking event driven network servers in JavaScript”.

How can I detect to tcp client connection error or network , I face the same problem as this issue. nodejs/node#4560 TCP socket stay in But, I can't receive any event when client network changed. In this blog post, we will build a simple social network with realtime features and a list of all members who are online. We will be using Node.js as the application server, Vanilla JavaScript in the front end and Pusher for realtime communication between our server and front end.

Decision Analytics Applications in Industry, But, I can't receive any event when client network changed. General help questions should be directed at the nodejs/help issue tracker  There is a EventId 4004 "Network State Change Event" that fires whenever a network connection is made or re-identified. There are less straightforward events in the NCSI log. The EventId 4042 Capability change tells you that this network discovery tool woke up and tried to figure out if you were on a real internet connection, on a domain network, or stuck behind a captive portal.

How to check if there is an active internet connection in Node.js, While leading to data redundancy, it greatly increases the network's efficiency and that prohibits the information from being changed as soon as it is recorded. “Node.js is an asynchronous event driven JavaScript runtime, designed to build  (i) A change in status described in paragraph (c)(2) of this section occurs; and (ii) The election change satisfies the consistency rule of paragraph (c)(3) of this section. (2) Change in status events. The following events are changes in status for purposes of this paragraph (c): (i) Legal marital status.

  • 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)
  • Check this link, this might help…
  • The package literally just pings Google:…