Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

connectivity-monitor

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

connectivity-monitor

Handle browser connectivity events

  • 0.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Connectivity Monitor

When creating applications that need to handle spotty connectivity or work offline, it is useful to have a way to check the connectivity and also monitor when the device connectivity changes.

General usage

  // Try to fetch user session, and if it fails because of connectivity, fetch from storage instead

  try {
    const response = await fetch('/user-session/');

    return await response.json();
  } catch (error) {
    if (await connectivityMonitor.getConnectionStatus() === 'disconnected') {
      return localStorage['/user-session/'];
    } else {
      throw error;
    }
  }
  // Monitor connectivity and warn the user when offline

  connectivityMonitor.listen((connectivityStatus) => {
    if (connectivityStatus === 'disconnected') {
      window.alert('Warning: your device is currently offline, so any changes will not be saved!');
    }
  });

ConnectivityMontior

The ConnectivityMonitor implementation leverages window.navigator.online, and will also call an API operation you provide to verify connectivity whenever the connectivity status changes or getConnectionStatus is called.

const connectivityMonitor = new ConnectivityMonitor(serviceProxy, '/connectivity-test/');

MockConnectivityMonitor

The MockConnectivityMonitor implementation is useful for testing and when running your app with mock data and mock services. It does not actually check connectivity, and instead relies on the connectivity status to be set manually via its setConnectionStatus method.

const connectivityMonitor = new ConnectivityMonitor(serviceProxy);

connectivityMonitor.setConnectionStatus('connected');
connectivityMonitor.setConnectionStatus('disconnected');

FAQs

Package last updated on 15 Dec 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc