pubnub / javascript

PubNub JavaScript SDK docs https://www.pubnub.com/docs/sdks/javascript
Other
553 stars 401 forks source link

Fix infinite heartbeat interval if presenceTimeout is set to 0 #195

Closed asjadsaboor-10p closed 3 years ago

asjadsaboor-10p commented 4 years ago

Question: What should be the value of HeartbeatInterval when presenceTimeout is 0 ?

yuhui-zhao commented 4 years ago

This pr does not cover the case that presenceTimeout is not set. Based on the v4.27.1 implementation, the value of the heartbeatInterval should be (PRESENCE_TIMEOUT_MINIMUM / 2 - 1 ) when presenceTimeout is 0

  setPresenceTimeout(val: number): this {
    if (val >= PRESENCE_TIMEOUT_MINIMUM) {
      this._presenceTimeout = val;
    } else {
      this._presenceTimeout = PRESENCE_TIMEOUT_MINIMUM;

      // eslint-disable-next-line no-console
      console.log(
        'WARNING: Presence timeout is less than the minimum. Using minimum value: ',
        this._presenceTimeout
      );
    }

    this.setHeartbeatInterval(this._presenceTimeout / 2 - 1);

    return this;
  }

The root cause of this is the change in config.js that does not give a default value to the heartbeatInterval.

    if (setup.presenceTimeout) {
      this.setPresenceTimeout(setup.presenceTimeout);
    } else {
      this._presenceTimeout = PRESENCE_TIMEOUT_DEFAULT;
    }

    if (setup.heartbeatInterval != null) {
      this.setHeartbeatInterval(setup.heartbeatInterval);
    }
ghost commented 3 years ago

This should be resolved due to the minimum value check which is currently set to 20