A lightweight browser utility to watch and react to online/offline network connection changes.
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.
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.");
}Creates a new NetworkConnectionWatcher instance and immediately starts listening for online and offline window events.
Returns true if the browser is currently online, false otherwise.
The opposite: Returns false if the browser is currently online, true otherwise.
Registers a callback that is invoked when the browser transitions to an online state. Returns the watcher instance to allow method chaining.
Registers a callback that is invoked when the browser transitions to an offline state. Returns the watcher instance to allow method chaining.
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.