Skip to content

MadeByRaymond/ngx-network-monitor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

7 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

ngx-network-monitor

A lightweight Angular service to monitor network status: online/offline, connection quality (2G/3G/4G/5G), and ping latency.

npm Angular NPM Downloads License


πŸš€ Features

  • βœ… Detects online/offline events using the browser API
  • βœ… Measures latency using configurable ping endpoint
  • βœ… Monitors effective connection type (5g, 4g, 3g, etc.)
  • βœ… Flags poor connections automatically
  • βœ… SSR-compatible & configurable with InjectionToken

πŸ“¦ Installation

npm install ngx-network-monitor

Or using Angular CLI:

ng add ngx-network-monitor

This will create a ping file in src/assets/ping.txt for you, assuming src/assets/** is a static file directory.


πŸ”§ Setup

By default, the service pings /assets/ping.txt every few seconds (depending on browser connection support). You can customize the ping URL to a different static file, endpoint or url:

import { PING_URL } from 'ngx-network-monitor';

@NgModule({
  providers: [
    { provide: PING_URL, useValue: '/your-api/ping' }
  ]
})
export class AppModule {}

βœ… Requirements for Ping Endpoint

Make sure your ping endpoint, url or file:

  • Returns a 200 or 204 response
  • Supports CORS (if it's on a different domain)
  • Responds quickly

🧠 Usage

Inject the service and subscribe to network status changes:

import { NetworkMonitorService, NetworkStatus } from 'ngx-network-monitor';

export class StatusComponent {
  status: NetworkStatus | null = null;

  constructor(private monitor: NetworkMonitorService) {
    this.monitor.networkStatus$.subscribe((status) => {
      this.status = status;
    });
  }
}

Full Usage Example:

import { Component } from '@angular/core';
import { NetworkMonitorService, NetworkStatus } from 'ngx-network-monitor';

@Component({
  selector: 'app-status',
  template: `
    <div *ngIf="status">
      <p>βœ… Online: {{ status.online }}</p>
      <p>⏱️ Latency: {{ status.latency }}ms</p>
      <p>πŸ“Ά Connection Type: {{ status.effectiveType || 'unknown' }}</p>
      <p>⚠️ Poor Connection: {{ status.poorConnection }}</p>
    </div>
  `
})
export class StatusComponent {
  status: NetworkStatus | null = null;

  constructor(private monitor: NetworkMonitorService) {
    this.monitor.networkStatus$.subscribe((status) => {
      this.status = status;
    });
  }
  
  get currentStatus() {
    return this.monitor.currentStatus
  }

  runManualCheck(){
    this.monitor.runManualCheck((newStatus) => {
      // Do anything with new status E.g:
      status = newStatus;
    })
  }
}

πŸ”‘ Additional Properties

Property Description
*.currentStatus Gets the current network status
*.runManualCheck(callback) Triggers the network status check manually and accepts an optional callback which returns the new status

πŸ“ Assets (for default setup)

Ensure this file exists in your app as a static file if using the default ping path:

/assets/ping.txt

If you prefer to ping a different static file / endpoint / url, you can change the default value as mentioned in the "πŸ”§ Setup" section:

import { PING_URL } from 'ngx-network-monitor';

@NgModule({
  providers: [
    { provide: PING_URL, useValue: '/your-api/ping' }
  ]
})
export class AppModule {}

βš™οΈ Configuration Summary

Feature Customizable Default
Ping URL βœ… PING_URL token /assets/ping.txt
Ping Interval βœ… auto-adjusts 10s (no connection API) / 60s (with)
Connection Type βœ… uses browser API Based on navigator.connection
Poor Connection βœ… auto-detected Slow type or latency > 1000ms

πŸ§ͺ Development

# Run tests
ng test ngx-network-monitor

# Build for production
ng build ngx-network-monitor

πŸ”’ License

MIT Β© MadeByRaymond (Daniel Obiekwe)


❀️ Support

If you find this package helpful, you can support our projects here:

Buy Me a Smoothie

About

A lightweight Angular service to monitor network status: online/offline, connection quality (2G/3G/4G/5G), and ping latency.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published