Skip to content

BitAndBlack/online-offline-handler-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

npm version

Bit&Black Logo

Bit&Black Online/Offline Handler

A lightweight browser utility to watch and react to online/offline network connection changes.

Installation

This library is made for the use with Node. Add it to your project by running $ npm install bitandblack-online-offline-handler or add it with another dependency manager by your choice.

Usage

Import the NetworkConnectionWatcher into your project:

import { NetworkConnectionWatcher } from "bitandblack-online-offline-handler";

Create a new instance and register your callbacks:

const networkConnectionWatcher = new NetworkConnectionWatcher();

networkConnectionWatcher
    .setOnGoingOnlineCallback((event) => {
        console.log("Back online!", event);
    })
    .setOnGoingOfflineCallback((event) => {
        console.log("Gone offline!", event);
    })
;

You can also check the current connection state at any time:

if (true === networkConnectionWatcher.isOnline()) {
    console.log("Currently online.");
} else {
    console.log("Currently offline.");
}

API

new NetworkConnectionWatcher()

Creates a new NetworkConnectionWatcher instance and immediately starts listening for online and offline window events.

isOnline(): boolean

Returns true if the browser is currently online, false otherwise.

isOffline(): boolean

The opposite: Returns false if the browser is currently online, true otherwise.

setOnGoingOnlineCallback(callback: (event: Event) => void): this

Registers a callback that is invoked when the browser transitions to an online state. Returns the watcher instance to allow method chaining.

setOnGoingOfflineCallback(callback: (event: Event) => void): this

Registers a callback that is invoked when the browser transitions to an offline state. Returns the watcher instance to allow method chaining.

Contact

If you have any questions, feel free to contact us under hello@bitandblack.com.

Further information about Bit&Black can be found at www.bitandblack.com.

About

A lightweight browser utility to watch and react to online/offline network connection changes.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors