Home

Awesome

Haywire

A minimal (less than 1k minified and gzipped) javascript library for network issues detection.

Build License

How does Haywire work?

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:

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

Need help?

send me an email or ask me via twitter