is-online

Check if the internet connection is up
GitHub
1.25k
Created 10 years ago, last commit 5 months ago
13 contributors
121 commits
Stars added on GitHub, month by month
0
0
12
1
2
3
4
5
6
7
8
9
10
11
2023
2024
Stars added on GitHub, per day, on average
Yesterday
=
Last week
+0.1
/day
Last month
+0.2
/day
Last 12 months
+0.1
/day
npmPackage on NPM
is-online
11.0.0
Monthly downloads on NPM
12
1
2
3
4
5
6
7
8
9
10
11
2023
2024
README

is-online

Check if the internet connection is up

Works in Node.js and the browser (with a bundler).

In the browser, there is already navigator.onLine, but it's useless as it only tells you if there's a local connection, and not whether the internet is accessible.

Install

npm install is-online

Usage

import isOnline from 'is-online';

console.log(await isOnline());
//=> true

API

isOnline(options?)

options

Type: object

timeout

Type: number
Default: 5000

Milliseconds to wait for a server to respond.

ipVersion

Type: number
Values: 4 | 6
Default: 4

The Internet Protocol version to use.

This is an advanced option that is usually not necessary to be set, but it can prove useful to specifically assert IPv6 connectivity.

How it works

The following checks are run in parallel:

  • Retrieve icanhazip.com (or ipify.org as fallback) via HTTPS.
  • Query myip.opendns.com and o-o.myaddr.l.google.com DNS entries. (Node.js only)
  • Retrieve Apple's Captive Portal test page (this is what iOS does). (Node.js only)

When any check succeeds, the returned Promise is resolved to true.

Proxy support

To make it work through proxies, you need to set up global-agent.

Maintainers

Related