connection-watcher

0.2.4 • Public • Published

connection-watcher

HTTP service and internet connection watcher library

1. Installation

npm

npm i connection-watcher

Copy node_modules/connection-watcher/dist/connection-watcher.min.js to the web server's public folder or use some tool for it (eg. Webpack).

Manual Download

Download connection-watcher.min.js to the right place.

wget https://raw.githubusercontent.com/BenjaminFerge/connection-watcher/master/dist/connection-watcher.min.js

2. Usage

1. Include the script

<script src="connection-watcher.min.js"></script>

2. Initialize the module

The module is accessible from the window object:

var connWatcher = window.connectionWatcher;

We need the options object that specifies the network event handlers and the connection watchers.

var options = {
    // Network event handlers
    onConnectionOpened: event => {
        console.log("Internet connection OPENED", event);
    },
    onConnectionClosed: event => {
        console.log("Internet connection CLOSED", event);
    },
    // Connection watchers
    connections: {
        lan: {
            url: "http://localhost:8080/examples",
            // Optionally you can provide fetchOptions, which is the same as the ES6 fetch parameters.
            fetchOptions: {
                method: "GET",
                mode: "same-origin",
                cache: "no-cache",
                redirect: "follow"
            },
            interval: 1000, // in ms (1 sec)
            onClosed: (report) => {
                console.log("LAN connection is DEAD. Details:", report);
            },
            onOpened: (report) => {
                console.log("LAN connection is ALIVE. Details:", report);
            }
        },
        conn1: {
            url: "http://localhost:8080/non-existing-url1.hu",
            interval: 2500,
            onClosed: (report) => {
                console.log("CONN1 connection is DEAD. Details:", report);
            },
            onOpened: (report) => {
                console.log("CONN1 connection is ALIVE. Details:", report);
            }
        }
        //, ...
    }
};
// Finally the initialization.
connWatcher.init(options);

3. Start watchers

connWatcher.startAll();

See the example html for more details.

To run the example:

git clone https://github.com/BenjaminFerge/connection-watcher && \
cd connection-watcher && \
php -S localhost:8080 # run some local web server

...and visit: http://localhost:8080

License: MIT

Package Sidebar

Install

npm i connection-watcher

Weekly Downloads

6

Version

0.2.4

License

MIT

Unpacked Size

14 kB

Total Files

8

Last publish

Collaborators

  • benjaminferge