Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect network changes using Node.js

Tags:

node.js

How can I listen for changes in the network connectivity?

Do you know any implementation or module that accomplish this?

I'm wondering if exists something similar to:

  reachability.on('change' function(){...});
  reachability.on('connect' function(){...});
  reachability.on('disconnect' function(){...});

I've googled it and couldn't find anything about it.

Any help is appreciated. Thank you

like image 835
rodrigoArantes Avatar asked Oct 08 '14 14:10

rodrigoArantes


People also ask

How to check internet connectivity in Node js?

function checkInternet(cb) { require('dns'). lookup('google.com',function(err) { if (err && err. code == "ENOTFOUND") { cb(false); } else { cb(true); } }) } // example usage: checkInternet(function(isConnected) { if (isConnected) { // connected to the internet } else { // not connected to the internet } });

How you can monitor a file for modifications in Node js?

For monitoring a file of any modification, we can either use the fs. watch() or fs. watchFile() function. The difference between the two is fs.

What is Node JS in networking?

The Node. js platform itself is billed as a solution for writing fast and scalable network applications. To write network-oriented software, you need to understand how networking technologies and protocols interrelate.

How to detect network speed using JavaScript?

How to detect network speed using JavaScript? To detect the network speed using javascript, we will use the following approach. Approach: Open the web page for which you want to know the connection speed. The page should be the one for which you want to add the javascript code for detecting the speed.

How to do a speed test in Node JS?

Use the following steps to install the module and do a speed test in node.js: Step 1: Creating a directory for our project and making that our working directory. Step 2: Use the npm init command to create a package.json file for our project.

How do I make a GET request using Node JS?

The simplest way to perform an HTTP request using Node.js is to use the Axios library: However, Axios requires the use of a 3rd party library. A GET request is possible just using the Node.js standard modules, although it's more verbose than the option above:

How to add JavaScript code for detecting the speed of image?

The page should be the one for which you want to add the javascript code for detecting the speed. Assign or set up the address of the image which you want to use for speed test to the variable. The variables for storing the test’s start time, end time, and download size should be created.


1 Answers

There is no such functionality built-in to node. You might be able to hook into the OS to listen for the network interface going up or down or even an ethernet cable being unplugged, but any other type of connectivity loss is going to be difficult to determine instantly.

The easiest way to detect dead connections is to use an application-level ping/heartbeat mechanism and/or a timeout of some kind.

If the network connectivity detection is not specific to a particular network request, you could do something like this to globally test network connectivity by continually pinging some well-connected system that responds to pings. Example:

var EventEmitter = require('events').EventEmitter,
    spawn = require('child_process').spawn,
    rl = require('readline');

var RE_SUCCESS = /bytes from/i,
    INTERVAL = 2, // in seconds
    IP = '8.8.8.8';

var proc = spawn('ping', ['-v', '-n', '-i', INTERVAL, IP]),
    rli = rl.createInterface(proc.stdout, proc.stdin),
    network = new EventEmitter();

network.online = false;

rli.on('line', function(str) {
  if (RE_SUCCESS.test(str)) {
    if (!network.online) {
      network.online = true;
      network.emit('online');
    }
  } else if (network.online) {
    network.online = false;
    network.emit('offline');
  }
});



// then just listen for the `online` and `offline` events ...
network.on('online', function() {
  console.log('online!');
}).on('offline', function() {
  console.log('offline!');
});
like image 81
mscdex Avatar answered Sep 22 '22 16:09

mscdex