2

Here it is a code to check application is online or offline :

 this.online$ = Observable.merge(
            Observable.of(navigator.onLine),
            Observable.fromEvent(window, 'online').map(() => true),
            Observable.fromEvent(window, 'offline').map(() => false)
        )
        this.online$.subscribe(isOnline=>{
            if(isOnline){
              console.log(isOnline);
            }else{
              console.log("you are offline");
              console.log(isOnline);
            }
        });

But it always return a true it means they are online but it's wrong result. I turned off system internet unlikely they return same result (true).

QuokMoon
  • 4,387
  • 4
  • 26
  • 50
  • 3
    Possible duplicate of [How to check whether user has internet connection or not in Angular2?](http://stackoverflow.com/questions/39571231/how-to-check-whether-user-has-internet-connection-or-not-in-angular2) – Pardeep Jain Mar 21 '17 at 12:26
  • But this one is not work in observable case. – QuokMoon Mar 21 '17 at 13:03
  • You can use the shorthand `.mapTo(true)` instead of `.map(() => true)`. You don't need to and it requires another import, but it's a nice shorthand. – Aluan Haddad Mar 21 '17 at 14:34

3 Answers3

6
import { fromEvent, merge, of } from 'rxjs';
import { mapTo } from 'rxjs/operators';


this.online$ = merge(
       of(navigator.onLine),
       fromEvent(window, 'online').pipe(mapTo(true)),
       fromEvent(window, 'offline').pipe(mapTo(false))
    );

this.online$.subscribe((isOnline) =>{
  if(isOnline) {
    console.log(isOnline);
    } else {
      console.log("you are offline");
      console.log(isOnline);
    }
});

This will emit true or false based on the browsers online status.

Arjun Yelamanchili
  • 577
  • 1
  • 3
  • 16
Ketan Chaudhari
  • 556
  • 6
  • 15
  • Nice answer. A note: I would highly recommend to surround "of(navigator.onLine)" with a RxJS "defer" ("defer(() => of(navigator.onLine))"). You never know when the subscription happens (perhaps the online status has already changed in time) – Pierre Chavaroche Mar 30 '23 at 11:06
3

I have the following code in my app:

// Adjust the imports if you use RxJS < 6.0.0-alpha.3.
import { BehaviorSubject, fromEvent } from 'rxjs';

/**
 * Whether the browser indicates that the device is online.
 */
export const onlineSubject = new BehaviorSubject<boolean>(true);

const handleOnlineChange = (online: boolean) => {
  if (online !== onlineSubject.getValue()) {
    onlineSubject.next(online);
  }
};

handleOnlineChange(navigator.onLine);

fromEvent(window, 'online').subscribe(() => handleOnlineChange(true));
fromEvent(window, 'offline').subscribe(() => handleOnlineChange(false));

Hope this does the trick for you.

Ivan
  • 1,317
  • 13
  • 23
2

Lightest version, that always return navigator.onLine value:

import { fromEvent, merge, of } from 'rxjs';
import { map } from 'rxjs/operators';

isOffline$ = merge(
  of(null),
  fromEvent(window, 'online'),
  fromEvent(window, 'offline')
).pipe(map(() => !navigator.onLine));
Cédric M.
  • 113
  • 1
  • 6