Dynamic Online/Offline Detection of Network Status with RxJS
The network status of an application is a crucial component of an application and its features. Being aware of any changes to the network status serves as a highly beneficial monitor for UI/UX purposes and provides the best user experience possible. When crafting highly powerful web applications, we may need to detect a user's network status for many reasons, such as to determine when to use cached data or to simply display a message/new screen to the user.
Challenges in detecting network status
"In Chrome and Safari, if the browser is not able to connect to a local area network (LAN) or a router, it is offline; all other conditions return true. So while you can assume that the browser is offline when it returns a false value, you cannot assume that a true value necessarily means that the browser can access the internet. You could be getting false positives, such as in cases where the computer is running a virtualization software that has virtual ethernet adapters that are always "connected." Therefore, if you really want to determine the online status of the browser, you should develop additional means for checking." -MDN Webdocs
Not only is navigator.onLine unreliable as a standalone tool, but it is also only a one-time check. One could use setTimeout() to accomplish multiple/periodic checks, but Tevpro recommends using the popular RxJS library to better "listen" to network statuses.
Furthermore, to more accurately identify when a user is online or offline, it is suggested to use both the window offline and window online events in conjunction with navigator.onLine. Navigator.onLine returns the status of the browser, whereas the window online/offline event is fired when the browser has gained/lost access to the network. Used together, your application will be able to detect network changes more precisely.
In your Angular application, create a Network Service to hold the network status's value. In this service, you will create an Observable that encompasses the status/value by merging multiple observables (window offline event, window online event, and navigator.onLine) into a single observable value. Once created, you are able to inject this service into various parts of your application and use this value by subscribing to the observable!
Network Service and the isOnline$ Observable
Using the value in the isOnline$ Observable
Detecting a user's network status and the change in their status can be tricky. It is exceedingly advantageous for any application to iron out its custom implementation and accuracy of the user's online status to provide an enhanced user experience.
Tevpro is a custom software development firm founded in Houston, Texas. We work with companies of all sizes to create powerful web, cross-platform, and native applications. We'd love to work with you or help answer any questions.