Sample snippet:
const networkConnected = new BehaviorSubject<boolean>(false);
setTimeout(networkConnected.next(true), 10000);
webSocket('ws://localhost:4949')
.pipe(
retryWhen(errors => errors.pipe(delay(10000), filter(() => networkConnected.value === true))),
repeatWhen(completed => completed.pipe(delay(10000), filter(() => networkConnected.value === true))),
tap(a => console.log('Connecting...'))
).subscribe(
message=> console.info(message),
error => console.error(error),
() => console.warn('Completed'),
);
I spent an hour searching but couldn't find anyone facing the same issue.
The goal is to reconnect a WebSocket whenever it drops connection, using retryWhen
. Additionally, we use repeatWhen
for cases where the WebSocket completes...
Now, I want to incorporate logic to ensure reconnection (retry/repeat) only happens when the internet connection is stable - indicated by the networkConnected
observable being true
. If it's false
, retries/reconnections should wait until it becomes true.
Would utilizing zip
or mergeMap
work here? Alternatively, adding a timer
that checks every second if the value is true with skipUntil
.
If you have a better solution, please feel free to share 😊