Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sindresorhus/is-online
Check if the internet connection is up
https://github.com/sindresorhus/is-online
browser connectivity detect internet internet-connection javascript network nodejs online ping reachability
Last synced: 4 days ago
JSON representation
Check if the internet connection is up
- Host: GitHub
- URL: https://github.com/sindresorhus/is-online
- Owner: sindresorhus
- License: mit
- Created: 2014-09-19T17:58:49.000Z (over 10 years ago)
- Default Branch: main
- Last Pushed: 2024-07-26T15:37:48.000Z (6 months ago)
- Last Synced: 2024-10-29T15:34:45.695Z (2 months ago)
- Topics: browser, connectivity, detect, internet, internet-connection, javascript, network, nodejs, online, ping, reachability
- Language: JavaScript
- Homepage:
- Size: 88.9 KB
- Stars: 1,239
- Watchers: 25
- Forks: 83
- Open Issues: 4
-
Metadata Files:
- Readme: readme.md
- License: license
- Security: .github/security.md
Awesome Lists containing this project
- awesome-nodejs-cn - is-online - 检测网络连接是否正常 (包 / 命令行程序)
- awesome-nodejs - is-online - Check if the internet connection is up. (Packages / Command-line apps)
- awesome-nodejs - is-online - Check if the internet connection is up - ★ 435 (Command-line apps)
- awesome-node - is-online - Check if the internet connection is up. (Packages / Command-line apps)
- awesome-nodejs-cn - is-online - 检查互联网连接是否正常. (目录 / 命令行应用)
- awesome-nodejs - is-online - 检查互联网连接是否建立 (Uncategorized / Uncategorized)
- awesome-nodejs-cn - is-online - **star:1246** 检查网络连接是否正常 (包 / 命令行程序)
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`](https://developer.mozilla.org/en-US/docs/Web/API/NavigatorOnLine.onLine), but it's useless as it only tells you if there's a local connection, and not whether the internet is accessible.
## Install
```sh
npm install is-online
```## Usage
```js
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](https://en.wikipedia.org/wiki/Internet_Protocol#Version_history) 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](https://github.com/major/icanhaz) (or [ipify.org](https://www.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`](https://github.com/gajus/global-agent).
## Maintainers
- [Sindre Sorhus](https://github.com/sindresorhus)
- [silverwind](https://github.com/silverwind)## Related
- [is-online-cli](https://github.com/sindresorhus/is-online-cli) - CLI for this module
- [is-reachable](https://github.com/sindresorhus/is-reachable) - Check if servers are reachable