Check if the internet connection is up
Last updated 4 years ago by icestone .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install on-line 
SYNC missed versions from official npm registry.

on-line Build Status

Check if the internet connection is up

Works in Node.js.

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


$ npm install --save on-line


The basic use

var isOnline = require('on-line');

isOnline(function(err, online) {
  // => true

Custom options

var isOnline = require('on-line');
isOnline({ timeout: 1000, server: '' }, function(err, online) {
  // => true

Node API



Type: object


Type: number

Milliseconds to wait for a server to send response.


Type: string

Testing server.

callback(error, online)

Type: function

error is there only by Node.js convention and is always null.

How it works

In node, we first contact one of the thirteen root servers and ask them to direct us to the servers which host the <root> zone (Which they are themselves). If the server answers, we return an online status.

If no satisfying answer is given within one second, we return an offline status. In the rare case where an firewall intercepts the packet and answers it on its behalf, a second check is run which tries to connect to a series of popular web sites on port 80. If one of these connects, we return online, otherwise offline status.

In the browser, a sophisticated check like in node is not possible because DNS and sockets are abstracted away. We use a check which requests an uncached favicon.ico on a series of popular websites. If one of this checks succeeds, we return online status. If all the requests fail, we return offline status.




Current Tags

  • 0.1.1                                ...           latest (4 years ago)

2 Versions

  • 0.1.1                                ...           4 years ago
  • 0.1.0                                ...           4 years ago
Maintainers (2)
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 3
Dependencies (0)
Dev Dependencies (2)
Dependents (1)

Copyright 2014 - 2016 © taobao.org |