Closed AntonioA-D-C closed 8 months ago
We've also experienced this issue where Pusher stops working and doesn't seem to produce any errors. A slight difference from @AntonioA-D-C's experience is that Pusher attempts to reconnect (ie: onConnectionStateChange. previousState=RECONNECTING newState=CONNECTING
) but never successfully reaches the CONNECTED
state.
LOG onSubscriptionSucceeded: name: private-xxxx-xxx
LOG onSubscriptionSucceeded: data: {}
LOG onSubscriptionSucceeded: name: private-xxxx-xxx
LOG onSubscriptionSucceeded: data: {}
LOG onConnectionStateChange. previousState=CONNECTED newState=DISCONNECTED
LOG onConnectionStateChange. previousState=DISCONNECTED newState=RECONNECTING
LOG onConnectionStateChange. previousState=RECONNECTING newState=CONNECTING
Not sure if it matters, but the above happened on the iOS simulator that was running since the night before. I only stumbled upon it this morning.
Is there an event callback such as onConnectionStateFailure
? So that we can manually trigger a reconnection with Pusher?
Because so far, it seems that the trail ends with newState=CONNECTING
and no error produced. Any help or insight would be greatly appreciated!
Specs:
@xutopia there's a known issue in our underlying Swift websocket library waiting rarely waiting indefinitely for the device to reconnect (but the iOS device has disposed of the connection. So it will wait forever, hence hanging on reconnecting). There's an experimental fork that should mitigate this.
@AntonioA-D-C Your case is more complex, and I suspect its because you are using a Context. I've used contexts in a provider before in React, but not in React Native. So although your app is establishing the connection and subscribing to the channel, the RN context probably isn't behaving as expecting when your client receives an event. Could you restructure your Pusher implementation away from using Contexts, and just initialize it in auseEffect(() => {...}, []);
like our example?
@benjamin-tang-pusher are there any plans to merge the changes from the experimental fork? we would like to migrate to the new library but with pusher being a critical step in our business operations, we cannot afford to risk the flaky connections.
@AntonioA-D-C Using a context with pusher will give you a very odd behaviour, you can refactor your code into a hook then consume where ever needed if possible.
So I can't use Pusher Data globally?
@xutopia there's a known issue in our underlying Swift websocket library waiting rarely waiting indefinitely for the device to reconnect (but the iOS device has disposed of the connection. So it will wait forever, hence hanging on reconnecting). There's an experimental fork that should mitigate this.
@AntonioA-D-C Your case is more complex, and I suspect its because you are using a Context. I've used contexts in a provider before in React, but not in React Native. So although your app is establishing the connection and subscribing to the channel, the RN context probably isn't behaving as expecting when your client receives an event. Could you restructure your Pusher implementation away from using Contexts, and just initialize it in a
useEffect(() => {...}, []);
like our example?
Alright, I stopped using context, and decided to initialize it in a useEffect like the example you mentioned
Now, Initially it works with zero issues, but when I travel to another screen, and come back it doesn't work anymore, why?
I also encountered a similar issue and after debugging I found out that
After hot reload pusher.channels
is still populated even though those channels are not connected
Not sure if this is the preferred approach but this is how I fixed it
const pusher = Pusher.getInstance()
const init = async () => {
// Rest of the code
if (pusher.channels.size > 0) {
const promises = [];
pusher.channels.forEach(({channelName}) =>
promises.push(pusher.unsubscribe({channelName})),
);
await Promise.allSettled(promises);
}
pusher.init({ ... })
// Rest of the code
}
The reason behind a large number of logs for CONNECTED
, DISCONNECTED
and all the other states is that ON_CONNECTION_STATE_CHANGE
listener is not being removed in removeAllListeners
Just add the following code in removeAllListeners
and the events will be logged only for one connection
this.pusherEventEmitter.removeAllListeners(
PusherEventName.ON_CONNECTION_STATE_CHANGE
);
If you want to use a patch file follow the steps below
patches
in your root directory@pusher+pusher-websocket-react-native+1.3.0.patch
1.3.0
is the version (latest) I am using make sure to check you are using in yourpackage.json
Past the following code in the file
diff --git a/node_modules/@pusher/pusher-websocket-react-native/src/index.tsx b/node_modules/@pusher/pusher-websocket-react-native/src/index.tsx
index 5c04965..6bff3b7 100644
--- a/node_modules/@pusher/pusher-websocket-react-native/src/index.tsx
+++ b/node_modules/@pusher/pusher-websocket-react-native/src/index.tsx
@@ -312,6 +312,9 @@ export class Pusher {
this.pusherEventEmitter.removeAllListeners(
PusherEventName.ON_MEMBER_REMOVED
);
+ this.pusherEventEmitter.removeAllListeners(
+ PusherEventName.ON_CONNECTION_STATE_CHANGE
+ );
}
public async reset() {
4. Run the following command
npx patch-package
You can read more about `patch-package` and how to configure it in your project [here](https://github.com/ds300/patch-package?tab=readme-ov-file#readme).
We recently updated the library to use a newer Swift dependency that resolves the issue mentioned in https://github.com/pusher/pusher-websocket-react-native/issues/111#issuecomment-1773252321
@AntonioA-D-C I can see you raised https://github.com/pusher/pusher-websocket-react-native/issues/123 as a follow up so I will close this thread
I solved the issue
The problem was I was calling Pusher with each re-render which caused pusher to act in erratic ways
This was fixed
Sorry for the inconvenience
onConnectionStateChange. previousState=DISCONNECTED newState=CONNECTING onConnectionStateChange. previousState=DISCONNECTED newState=CONNECTING onConnectionStateChange. previousState=DISCONNECTING newState=DISCONNECTED onConnectionStateChange. previousState=DISCONNECTING newState=DISCONNECTED onConnectionStateChange. previousState=CONNECTING newState=CONNECTED onConnectionStateChange. previousState=CONNECTING newState=CONNECTED
This is the connection data on my console
And this is the data that tells me the subscription was successful
When I try sending data be it through postMan or the App, it refuses to do a single thing, doesn't give me any errors either, it only works sometimes.
This might seem vague, but I haven't genuinely gotten any signal of why this is happenning, I'm logging every possible action that could be responsible, it just seems like the onEvent method is never triggered at all, yet sometimes it works perfectly.
The context