Browse documentation

NetworkManager

Observes browser connectivity and signal quality.

Table of contents

Category

Value

Import

js
import { NetworkManager } from "valyrian.js/network";

Public signature

ts
new NetworkManager(options?: NetworkManagerOptions): NetworkManager

API form

NetworkManager is a class. Construct one manager for the connectivity lifetime observed by the application and destroy it when that lifetime ends.

Options and status

ts
type NetworkConnection = {
  effectiveType?: string;
  downlink?: number;
  rtt?: number;
  saveData?: boolean;
  addEventListener?: (event: string, listener: () => void) => void;
  removeEventListener?: (event: string, listener: () => void) => void;
};

type NetworkManagerOptions = {
  runtime?: {
    isNodeJs?: boolean;
    navigator?: {
      onLine: boolean;
      connection?: NetworkConnection;
      mozConnection?: NetworkConnection;
      webkitConnection?: NetworkConnection;
    } | null;
    window?: {
      addEventListener?: (event: string, listener: () => void) => void;
      removeEventListener?: (event: string, listener: () => void) => void;
    } | null;
  };
};

type NetworkStatus = {
  online: boolean;
  effectiveType?: string;
  downlink?: number;
  rtt?: number;
  saveData?: boolean;
};
type NetworkListener = (status: NetworkStatus) => void;

Methods and events

ts
getNetworkStatus(): NetworkStatus
getStatus(): NetworkStatus
getSignalLevel(): SignalLevel
isConnectionPoor(): boolean
on(event: NetworkEvent, listener: NetworkListener): () => void
off(event: NetworkEvent, listener: NetworkListener): void
destroy(): void

Runtime defaults

The browser uses navigator and the optional Network Information API. Without connection metrics, status still reports online. In Node.js, status is online and the signal level is Good. isConnectionPoor() is true only for SignalLevel.None.

Errors

on(...) and off(...) throw NetworkError for an invalid event type. Missing navigator or Network Information data falls back to the available online status.