Retry Wamp connection when it fails

How to manually reconnect in the onclose callback if will_retry is false ?

unfortunately, currently, while you can cancel an auto-reconnect attempt if auto-reconnect is enabled by returning “true” from onclose

you cannot enforce a (manual) auto-reconnect when auto-reconnect is NOT enabled

you can of course just overwrite “_retry” before returning “false” from onclose …

@oberstet

Would you mind sharing how would you do it ? Like providing an example. I think many others have been hitting this same wall.

The way I’m doing is like this:

 // establish connection using autobahnjs
  async initializeConnection() {
    this.globalSubscriptions = []
    this.initialSessionOpen = new Promise((resolve, reject) => {
      // connect to wamp
      this.connection = new autobahn.Connection({
        url: this.sessionConnectUrl,
        realm: "ninja",
        authmethods: ["wampcra", "anonymous"],
        authid: this._getUser(),
        max_retries: -1,
        onchallenge: (session, method, extra) => {
          if (method === "wampcra") {
            return autobahn.auth_cra.sign(this.userUid, extra.challenge)
          }
        },
      })

      this.connection.onopen = (session, details) => {
        if (!this.session) {
          this.session = session
        }

        if (!!this.globalSubscriptions.length) {
          this.globalSubscriptions.forEach(sub => session.subscribe(sub.topic, sub.handler))
        }
        resolve()
      }

      this.connection.onclose = (reason, details) => {
        this.disconnectDetails = `${reason} ${JSON.stringify(details)}`
        this.session = null

        resolve() // resolve even if error, autobahn will retry.

        // Force autobahn to always retry.
        if (!details.will_retry) {
          clearTimeout(this.retryTimeoutId)
          this.retryTimeoutId = setTimeout(() => this.initializeConnection(), 1000)
        }
      }

      this.connection.open()
    })
  }