redux-saga-network-status
Network status detection with server ping and backoff for redux-saga.
Monitors network status using a combination of NavigatorOnLine.onLine, Online and offline events, and pinging a server (with an HTTP GET request). Server pings are retried using a fibonacci backoff interval with a randomization factor to prevent network congestion.
Installation
npm install redux-saga-network-status
Usage
redux-saga-network-status exports its saga by default. Start it as part of your root saga, e.g.
// sagas.js; { ; // ... more sagas}
And connect the reducer to your root reducer:
// reducers.js;; network // ... more reducers;
// main.js;; ;; const sagaMiddleware = ;const store = ; sagaMiddleware;
Then dispatch the startWatchNetworkStatus
action to begin monitoring network status using the provided ping URL:
import actions as networkActions from 'redux-saga-network-status'; { const startWatchNetworkStatus = thisprops; ; } { /* ... */ } null startWatchNetworkStatus: networkActionsstartWatchNetworkStatusAppContainer;
Reducer state
The network
reducer we connected above will provide the following state in your store:
// True when we have been online at least oncestatenetworkhasBeenOnline;// Whether we have pinged the server at least oncestatenetworkhasDetectedNetworkStatus;// Whether the browser is connected to a networkstatenetworkisNavigatorOnline;// Whether the server is reachable over the networkstatenetworkisOnline;// Whether we are currently pinging the serverstatenetworkisPinging;// Number of milliseconds until the next ping attemptstatenetworkmsUntilNextPing;// The most recent ping errorstatenetworkpingError;