angularwebsocketrxjsrxjs-observablesretrywhen

RXJS: How to get connect event? retrywhen: how to hook into execution?


When Angular tries to connect, I want to show "Connecting". I can do it on first connect, but I don't know how to do it when using retryWhen(). I need to hook into the actual execution and then do:

this.connectionStatus = "Connecting"

Current code:

    this.connectionStatus = "Connecting";

    let socket = new WebSocketSubject(..);

    this.socket
      .pipe(retryWhen(errors =>
        errors.pipe(
          tap(val => {
            console.log("Retry in 10 sec", val);
          }),
          delay(10000)
        )))
      .subscribe(..);

Solution

  • Just set the status after delay. retryWhen expect you to return an observable and when this observable emits it'll start retrying connecting i.e start retrying after delay

      errors.pipe(
              tap(val => {
                console.log("Retry in 10 sec", val);
              }),
              delay(10000),
              tap(()=>this.connectionStatus = "Connecting"
    )
            )))