YouTube Icon

How to detect network connection is Online/Offline Status In Angular




How to detect network connection is Online/Offline Status In Angular

We all might be familiar with the JavaScript way of checking Online/ Offline status. But in the case of Angular, we need to duly unsubscribe the events we're harkening to, else, we might beget gratuitous actions and memory leaks. 

Plain JavaScript

window.addEventListener("load", () => {
  this.internetStatus = navigator.onLine

  window.addEventListener("online", () => {
    this.internetStatus = true
  });

  window.addEventListener("offline", () => {
    this.internetStatus = false
  });
});

Angular

  • app.component.html
<h1>Internet Status</h1>

<div
  class="widget-content-online header-user-info ml-3"
  *ngIf="internetStatus"
>
  &#9679; Online
</div>
<div
  class="widget-content-offline header-user-info ml-3"
  *ngIf="!internetStatus"
>
  &#9679; Offline
</div>
  • app.component.ts
import { Component, OnDestroy, OnInit, VERSION } from '@angular/core';
import { fromEvent, merge, of, Subscription } from 'rxjs';
import { map } from 'rxjs/operators';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit, OnDestroy {
  internetStatus: boolean = false;
  internetStatus$: Subscription = Subscription.EMPTY;

  constructor() {}

  ngOnInit(): void {
    this.checkInternetStatus();
  }

  ngOnDestroy(): void {
    this.internetStatus$.unsubscribe();
  }

  checkInternetStatus() {
    this.internetStatus = navigator.onLine;
    this.internetStatus$ = merge(
      of(null),
      fromEvent(window, 'online'),
      fromEvent(window, 'offline')
    )
      .pipe(map(() => navigator.onLine))
      .subscribe(status => {
        console.log('status', status);
        this.internetStatus = status;
      });
  }
}
  • styles.css
.widget-content-online {
	padding: 3px 11px 5px 11px;
	border-radius: 52px;
	width: 90px;
	background: rgba(67, 164, 90, 0.15);
	font-size: 15px;
	color: #43A45A;
	text-align: center;
}

.widget-content-offline {
	padding: 3px 11px 5px 11px;
	border-radius: 52px;
	width: 90px;
	background: rgba(165, 83, 83, 0.15);
	font-size: 15px;
	color: #e52525;
	text-align: center;
}

Here you can see the Demo: https://check-online-offline-status-in-angular.stackblitz.io

Here you check the code: https://github.com/glokesh94/check-online-offline-status-in-angular

Follow on youtube channel for more videos: Youtube Channel

Here you can see youtube video: Detect network connection is Online/Offline Status Using Angular



Author Biography.

Lokesh Gupta
Lokesh Gupta

Overall 3+ years of experience as a Full Stack Developer with a demonstrated history of working in the information technology and services industry. I enjoy solving complex problems within budget and deadlines putting my skills on PHP, MySQL, Python, Codeigniter, Yii2, Laravel, AngularJS, ReactJS, NodeJS to best use. Through Knowledge of UML & visual modeling, application architecture design & business process modeling. Successfully delivered various projects, based on different technologies across the globe.

Join Our Newsletter.

Subscribe to CrowdforThink newsletter to get daily update directly deliver into your inbox.

CrowdforGeeks is where lifelong learners come to learn the skills they need, to land the jobs they want, to build the lives they deserve.

CrowdforGeeks

CrowdforThink is a leading Indian media and information platform, known for its end-to-end coverage of the Indian startup ecosystem.

CrowdforThink

Our mission is "Har Koi Dekhe Video, Har Ghar Dekhe Video, Ghar Ghar Dekhe Video" so we Provide videos related to Tutorials, Travel, Technology, Wedding, Cooking, Dance, Festivals, Celebration.

Apna Video Wala
CFT

News & Blogs

f744dbf3ab03eaaa58fd3e6dac2d0169.png

How to Get User Country Name, Browser, OS Name,...

Using PHP get stoner details IP address country, device, cybersurfer, operating system  &nb...

ff7f9eca4d1a0d29656644f11b806865.png

How to Drag And Drop Data Between Two Tables In...

Introduction  occasionally you need to drag and drop data from one table to another table...

fd0e5c6e4196603ef85979b1bc50caf4.jpeg

Top 10 Amazing Books For Every Designers

There are innumerous books out there that can help you come a better developer, whether you are l...

Top Authors

Lamia Rochdi is the Marketing Manager at Bell Flavors & Fragrances EMEA. A successful family-...

Lamia Rochdi

I’m Mertin Wilson a technician in a camera company and certified expert of different P...

Mertin Wilson

Zakariya has recently joined the PakWheels team as a Content Marketing Executive, shortly after g...

Zakariya Usman

Pankaj Singh is a Senior Digital Marketing Consultant with more than 2 years of experience in SEO...

Pankaj Singh
CFT

Our Client Says

WhatsApp Chat with Our Support Team