pusher / pusher-websocket-react-native

React Native official Pusher SDK
MIT License
62 stars 55 forks source link

Already subscribed to a channel with name. #82

Closed Bayramito closed 1 year ago

Bayramito commented 1 year ago

Hi,

I am just trying to make a presence connection to a channel with room_id but sometimes getting this error below.

WhatsApp Image 2023-03-28 at 11 16 49

How to handle connection properly.

My flow looks like this. Im handling these in a MobX store


@action subscribePusherInstance = () => {
    if (!this.contactId || this.isConnected) {
      console.log("PUSHER ALREADY CONNECTED OR NO CONTACT ID, EXITING");
      return;
    }

    // if alredy subscribed, unsubscribe first
    if (pusher.connectionState === "CONNECTED") {
      this.unsubscribePusherInstance().catch(console.warn);
    }

    pusher
      .connect()
      .then(r => {
        return pusher.subscribe({
          channelName: channelName + this.contactId,
          eventName,
          onEvent: this.onEvent,
          onSubscriptionSucceeded: this.onSubscriptionSucceeded,
          onSubscriptionFailed: this.onSubscriptionFailed,
          onMemberAdded: this.onMemberAdded,
          onMemberRemoved: this.onMemberRemoved,
          onSubscriptionError: this.onSubscriptionError,
        });
      })
      .then(r => {
        if (r.channelName && r.channelName === channelName + this.contactId) {
          this.isConnected = true;
        }
      })
      .catch(console.warn);
  };

and this is the unsubscription part


  @action unsubscribePusherInstance = () => {
    if (!this.contactId) return;
    return pusher
      .unsubscribe({channelName: channelName + this.contactId})
      .then(r => {
        this.isConnected = false;
        // return pusher.disconnect();
      })
      .catch(console.warn);
  };

If there is something wrong please let me know...

But this is working most of the time. The error shown in the image is so random... Which make it very weird...

Bayramito commented 1 year ago

no solution for this ?

fbenevides commented 1 year ago

This should be fixed on v1.2.2. Could you please test it? @Bayramito

Bayramito commented 1 year ago

This should be fixed on v1.2.2. Could you please test it? @Bayramito

yes sure