pusher / pusher-websocket-react-native

React Native official Pusher SDK
MIT License
61 stars 52 forks source link

Connection lost without any trigger #135

Open jahanzaib5244 opened 7 months ago

jahanzaib5244 commented 7 months ago

I am using two different simulators at the same time once the pusher is initialized and subscribe to a private channel it works perfectly but after one minute or more it stops listing without any trigger and sometimes I get an error and after some time it auto-reconnect what causes the problem and how I can resolve this

onError: Pong reply not received code: 4201 exception: <PusherSwift.PusherError: 0x600000d73db0>

first, it will work perfectly and after 30 or 40 seconds it stops listing for new events and does not give any state change event Screenshot 2024-02-07 at 2 08 21 PM

but after some time I got a new console like Screenshot 2024-02-07 at 2 10 18 PM

it automatically connects and disconnect again and again until I reset pusher Screenshot 2024-02-07 at 2 15 28 PM

Here is my configuration code for the pusher

export const Connect = async () => {

 return await pusher.init({
    apiKey: PUSHER_API_KEY,
    cluster: PUSHER_CLUSTER,
    useTLS: false,
    onConnectionStateChange,
    onAuthorizer: onAuthorizer,
    onError,
    onEvent,
    onSubscriptionSucceeded,
    onSubscriptionError,
    onSubscriptionCount,
    onDecryptionFailure,
    onMemberAdded,
    onMemberRemoved,
    maxReconnectionAttempts: 100,
    maxReconnectGapInSeconds: 0.01,
  })
}

 const initPusher = async () => {
    if (pusher.channels.size > 0) {
      const promises: any[] = [];

      pusher.channels.forEach(({channelName}) =>
        promises.push(pusher.unsubscribe({channelName})),
      );

      await Promise.allSettled(promises);
    }
    try {
      await Connect();
      if (pusher?.connectionState !== 'CONNECTED') {
        await pusher.subscribe({
          channelName: `private-chatify.${current_user_id}`,
          onEvent: (event: PusherEvent) => {
            console.log(event.eventName, event);
            if (event?.eventName === 'messaging') {
              setMessages(pre => [JSON.parse(event.data)?.api_message, ...pre]);
            }
          },
        });
        await pusher.connect();
      }
    } catch (error) {
      console.log(error);
    }
  };

useFocusEffect(
    useCallback(() => {
     initPusher();   
   }, []),
  );

@pusher/pusher-websocket-react-native": "^1.3.0" react native : "0.72.5" x code Version 15.2

benw-pusher commented 7 months ago

The error you are receieving (pong reply not received from server) would indicate that the client has been unable to check in with the server and so has terminated the connection. This would normally be expected when the network conditions have degraded, and certainly wouldn't be expected with as much frequency as you are experiencing. It could be that there is a a proxy, or other network device, that is closing the connection uncleanly. Often this is because such devices detect the connection as being idle. If this is the case you can use the https://github.com/pusher/pusher-websocket-react-native?tab=readme-ov-file#activitytimeout-double option to ensure the connection is active. As you are experiencing the disconnection after 30-40 seconds it may be prudent to try a value of 20 seconds and see if this resolves the issue.

jahanzaib5244 commented 7 months ago

The error you are receieving (pong reply not received from server) would indicate that the client has been unable to check in with the server and so has terminated the connection. This would normally be expected when the network conditions have degraded, and certainly wouldn't be expected with as much frequency as you are experiencing. It could be that there is a a proxy, or other network device, that is closing the connection uncleanly. Often this is because such devices detect the connection as being idle. If this is the case you can use the https://github.com/pusher/pusher-websocket-react-native?tab=readme-ov-file#activitytimeout-double option to ensure the connection is active. As you are experiencing the disconnection after 30-40 seconds it may be prudent to try a value of 20 seconds and see if this resolves the issue.

thank you for your response let me check if is it working in my case or not.