Awesome
Haywire
A minimal (less than 1k minified and gzipped) javascript library for network issues detection.
How does Haywire work?
-
Checks the availability of your site by pinging a healthcheck url.
-
Remembers past results so it can tell between offline, online and "flaky" (intermittent) conections.
-
Uses an exponential backoff policy so it doesn't flood your servers with requests.
-
It's fully configurable.
-
check the demo app!
A trivial example
To start checking your connection status:
Haywire.start({ 'callback': function (status) {
console.log('connection status is:' + JSON.stringify(status));
}});
Haywire periodically makes an http request: GET /healthcheck
, using XMLHttpRequest
. If the call returns a 200 OK
status (everything's fine), it increases the interval time and tries again in the future. Every time the status is checked, your callback
function is invoked.
Another example
function reportNetworkStatus(status) {
if (status.id === 0){
console.log('looking good :)');
} else if (status.id === 1) {
console.log('connection looks a bit unstable :S');
} else {
console.log('you\'re offline :(');
}
}
var opts = {
ping: { verb: 'HEAD', path: '/heartbeat'},
interval: 500,
limit: 8000,
onChange: reportNetworkStatus
}
Haywire.start(opts);
In this example, not only do you set the callback function, but also:
interval
how often do you ping (initially), in millisecondslimit
if the ping is succesful, the interval is doubled (backoff), the limit is the maximum value between pings.ping.verb
andping.path
these define your healthcheck url.
Configuration and defaults
These are the configuration options you can pass to Haywire, with their defaults:
{
'threshold': 4, // number of requests that should fail/succeed before considering the connection to be offline/online
'ping': {
'verb': 'GET', // ping request http verb
'path': '/healthcheck', // ping request path
'timeout': 1500, // ping request connection timeout in milliseconds
'status': 200 // expected http status code from the ping response
},
'interval': 500, // initial ping interval
'limit': 8000, // maximum ping interval
'onChange': function (status) {}, // callback executed after every ping
'backoffPolicy': function (result, last, interval, options) {} // backoff policy
}
How to get it
You can either fetch the latest release from the github releases tab or download it directly from bower:
$> bower install haywire