# \[Ionic 4\] Checking network status over Browser

**URL:** <https://forum.ionicframework.com/t/ionic-4-checking-network-status-over-browser/157648>\
**Category:** Ionic Framework\
**Created:** [February 26, 2019, 5:47pm UTC](https://forum.ionicframework.com/t/ionic-4-checking-network-status-over-browser/157648 "2019-02-26T17:47:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rodrigojrmartinez](https://avatars.discourse-cdn.com/v4/letter/r/e79b87/32.png) [@rodrigojrmartinez](https://forum.ionicframework.com/u/rodrigojrmartinez)\
**Post date:** [February 26, 2019, 5:47pm UTC](https://forum.ionicframework.com/t/ionic-4-checking-network-status-over-browser/157648/1 "2019-02-26T17:47:53Z")

</div>

Hi Guys

I’m developing an ionic 4 app where I need to keep track of the current network status and notify the user whenever the status changes. The app should be supported on browser besides mobile devices, so after checking a while I’ve learned that when on mobile, the Network cordova plugin in should be used. This works just excellent! My problem starts when having to detect this at browser level.

Basically, what I do is to wait for the platform to be ready, check where I’m at, and based on this listen for the proper events besides checking the status at that point, something like.

```auto
constructor(platform: Platform, network: Network) {
   this.platform.ready().then(() => {
      if (this.platform.is('cordova')) {
          // this is ok
          this.listenForDeviceNetwork();
      } else {
          this.listenForBrowserNetwork();
     }
   });
}

private listenForBrowserNetwork(){
    // also tried with document!
    window.addEventListener('offline', () => { console.log('now you are offline!'); });
    window.addEventListener('online', () => { console.log('now you are online!'); });
     this.isOnline = navigator.onLine;
}

```

The problem I’m facing is that the navigator.onLine is always returning true, and the events are never fired. Tryied taking out the internet connection as well as setting to the airplane mode, but noting… Also tried to log the navigator.onLine with an interval and check its value, but it is always returning true

```auto
setInterval(() => {
    console.log('now ', navigator.onLine);
}, 200);

```

Does anyone stumbled across this problem? Thanks for the time!

---

<div class="post-metadata">

**Author:** ![morphist](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/morphist/32/127474_2.png) [@morphist](https://forum.ionicframework.com/u/morphist)\
**Post date:** [February 26, 2019, 8:35pm UTC](https://forum.ionicframework.com/t/ionic-4-checking-network-status-over-browser/157648/2 "2019-02-26T20:35:52Z")

</div>

Here is how my network monitor works:

```auto
// NETWORK MONITOR:
  initNetworkMonitor() {
    console.log("init network monitoring...")
    if ('onLine' in navigator) {
      this.appIsOnline = navigator.onLine;
    }
    // check if we are on device or if its a browser:
    if (this.appIsOnDevice) {
      // watch network for a disconnect
      this.network.onDisconnect().subscribe(() => {
        console.log("network disconnected:(");
        this.appIsOnline = false;
      });
      // watch network for a connection
      this.network.onConnect().subscribe(() => {
        console.log("network connected!");
        this.appIsOnline = true;
        if (this.network.type === "wifi") {
          console.log("we got a wifi connection, woohoo!");
        }
        this.sync();
        if (this.sockets) {
          this.sockets.open()
        }
      });
      console.log("device network monitor is ON")
    } else {
      fromEvent(window, "offline").subscribe(() => {
        this.appIsOnline = false;
        console.log("network disconnected:(");
        this.fbdb.afd.database.goOffline();
      }
      );
      fromEvent(window, "online").subscribe(() => {
        this.appIsOnline = true;
        console.log("network connected!");
        this.fbdb.afd.database.goOnline();
        this.sync();
        if (this.sockets) {
          this.sockets.open()
        }
      }
      );
      console.log("PWA network monitor is ON")
    }
    if (!this.appIsOnline) {
      this.fbdb.afd.database.goOffline();
    }
  };

```

---

<div class="post-metadata">

**Author:** ![sampath](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/sampath/32/137165_2.png) [@sampath](https://forum.ionicframework.com/u/sampath)\
**Post date:** [July 22, 2019, 8:41pm UTC](https://forum.ionicframework.com/t/ionic-4-checking-network-status-over-browser/157648/3 "2019-07-22T20:41:17Z")

</div>

Here is the answer: [https://stackoverflow.com/questions/57153150/network-plugin-doesnt-work-on-pwa-app-ionic-4](https://stackoverflow.com/questions/57153150/network-plugin-doesnt-work-on-pwa-app-ionic-4)
