How to detect network status with nodejs?

nodejs can use the libraries Internet available and is online to check the network status. Its principle is to detect the dns connection status. These two libraries provide timeout detection.

 

 

Detect network status with nodejs:

internet-available

The principle of this library to detect the Internet connection state is to detect the dns connection state.

There must be a question here. Isn't it simpler to use nodejs native module dns?

You are right. nodejs does provide such a method, but the dns native module does not provide timeout detection. Internet available can set the timeout parameter, which is 5000ms by default (relying on the dns socket library to realize dns timeout. If you are interested, you can study its source code, which will not be expanded here).

Examples of using Internet available:

var internetAvailable = require("internet-available");
 
internetAvailable().then(function(){
 console.log("Internet available");
}).catch(function(){
 console.log("No internet");
});

If you want to add detection times and timeout time of each detection, the code is as follows:

var internetAvailable = require("internet-available");
 
internetAvailable({
 timeout: 4000,
 retries: 10,
}).then(function(){
 console.log("Internet available");
}).catch(function(){
 console.log("No internet");
});

The default DNS domain name detected by Internet available is Google COM, if you want to customize the domain name, the code is as follows:

var internetAvailable = require("internet-available");
 
internetAvailable({
 domainName: "xxxxx.com",
 port: 53,
 host: '8.8.8.8' // Default, please change to 114.114.114.114 in China
}).then(() => {
 console.log("Internet available");
}).catch(() => {
 console.log("No internet");
});

Note: 8.8.8.8 is a free DNS server provided by Google. The address is universal. Relatively speaking, it is more suitable for foreign users and users visiting foreign websites, and 114.114.114.114 is more suitable for domestic users.

Computer embroidery factory http://www.szhdn.com Guangzhou brand design companyhttps://www.houdianzi.com

is-online

The detection method of is online is the same as that of Internet available. The only difference is that is online can run in nodejs and browser environment at the same time. In the browser environment, through navigator Online returns the network connection status, but like html5 online and offline events, it can only detect local connections.

Is online usage example

const isOnline = require('is-online');
  
isOnline().then(online => {
 if(online){
 console.log("We have internet");
 }else{
 console.log("Houston we have a problem");
 }
});

This library also provides timeout settings and can set the Internet Protocol version. This is an advanced option that usually does not need to be set, but it is very useful for specifically asserting IPv6 connections. The code is as follows

var isOnline = require('is-online');
  
isOnline({
 timeout: 5000,
 version: "v4" // v4 or v6
}).then(online => {
 if(online){
 console.log("Internet available");
 }else{
 console.log("No internet");
 }
});

Tags: Javascript

Posted by eazyefolife on Wed, 11 May 2022 09:45:47 +0300