UNPKG

ng-connection-monitor

Version:

A lightweight Angular service to detect real internet connection using a backend ping, not navigator.onLine.

86 lines (71 loc) โ€ข 2.46 kB
# ๐ŸŒ Angular Network Status A lightweight Angular library to detect actual internet connectivity by pinging a backend endpoint โ€” **not just relying on `navigator.onLine`**. ## ๐Ÿš€ Why This Library? - `navigator.onLine` is unreliable โ€” it only checks local connection, not real internet access. - This library actively **pings a backend server** to verify actual connectivity. - Exposes a clean **RxJS Observable** (`isOnline$`) to subscribe from any component or service. - Fully compatible with **Angular v13 and above**. --- ## ๐Ÿ“ฆ Installation ```bash npm install ng-connection-monitor ``` ## โš™๏ธ Setup ### 1. Import HttpClientModule in your AppModule ```bash import { HttpClientModule } from '@angular/common/http'; @NgModule({ imports: [ HttpClientModule, // other imports ], }) export class AppModule {} ````` ### 2. Import NgConnectionMonitorModule in your AppModule ```bash import { NgConnectionMonitorService } from './ng-connection-monitor.service'; @NgModule({ declarations: [], imports: [ CommonModule ], providers:[NgConnectionMonitorService] }) export class NgConnectionMonitorModule { } ```` ### 3.Use the NgConnectionMonitorService in your component ```bash import { NgConnectionMonitorService } from 'ng-connection-monitor'; // library import @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { title = 'demo'; onlineStatus:any constructor(private networkStatusService: NgConnectionMonitorService){ this.networkStatusService.isOnline$.subscribe(data=>{ this.onlineStatus = data console.log("data",data) }); } } ``` ## ๐Ÿงช How It Works - Starts a timer loop every 3 seconds - Pings the configured backend endpoint (default: /api/ping) - If response is 200, emits true, else false - Keeps emitting changes only when status flips (distinctUntilChanged) ## โœ… API ```bash isOnline$ : Observable<boolean> ``` ##### Subscribe to this to get real-time connectivity status. ## ๐Ÿ’ก Roadmap / Future Ideas - Detect slow internet & emit custom message (e.g., "Slow Connection Detected") - Support multi-ping strategies - Angular signals support (v17+) ## ๐Ÿ›ก๏ธ License Let me know if you want to replace [Sourav](https://github.com/souravion) with your actual GitHub handle or real name.