Detect online/offline state
Angular 5+ Module to track online and offline state
https://vadimdez.github.io/ngx-online-status/
or stackblitz code/demo
https://stackblitz.com/edit/ngx-online-status
npm install ngx-online-status --save
- Import
OnlineStatusModule
to your module
import { OnlineStatusModule } from 'ngx-online-status';
@NgModule({
declarations: [
// ...
],
imports: [
OnlineStatusModule
],
providers: [
//...
]
})
- Inject
OnlineStatusService
and use it:
import { OnlineStatusService, OnlineStatusType } from "ngx-online-status";
export class AppComponent {
status: OnlineStatusType = this.onlineStatusService.getStatus(); // get initial status
constructor(private onlineStatusService: OnlineStatusService) {
this.onlineStatusService.status.subscribe((status: OnlineStatusType) => {
// use status
this.status = status;
});
}
}
OnlineStatusType.OFFLINE
equals to 0
OnlineStatusType.ONLINE
equals to 1
MIT ยฉ Vadym Yatsyuk