xmtp / xmtp-react-native

A package you can use to build with XMTP in a React Native or Expo app.
MIT License
41 stars 19 forks source link

Detecting when a stream connection is lost #108

Open nplasterer opened 1 year ago

nplasterer commented 1 year ago

Other question : from the current implementation of the streaming (of new conversations & messages), is there a way to know when the connection is “lost” somehow? It’s important to us for 2 things: show the user that we’re currently trying to reconnect resync information that might have been lost during this disconnect period

We already have a way to detect full disconnect on the phone (the OS tells us internet is not reachable anymore) but it’s not the same as connection failing for a short while

richardhuaaa commented 1 year ago

@nplasterer Does this error not show during a disconnect? https://github.com/xmtp/libxmtp/blob/v2/bindings_swift/src/lib.rs#L136

I'm wondering if the keepalive we added will automatically address this issue

nmalzieu commented 1 year ago

@richardhuaaa just adding here: my goal is to catch in real time that information directly in the RN SDK to show something to the user (a loader / a "connecting..." message)

richardhuaaa commented 1 year ago

@nmalzieu The grpc keepalive is being reduced to 3s here: https://github.com/xmtp/libxmtp/pull/267. I'm becoming more confident that this is a reasonable approach given this description in the docs: https://grpc.io/docs/guides/keepalive/#common-situations-where-keepalives-can-be-useful

With a 3s timeout, most disconnects should recover near instantly, depending where in the 3s cycle the disconnect happens, and if the disconnect itself was longer than 3s.

Once that change is deployed, I'm wondering if this error will show within 3s on disconnect: https://github.com/xmtp/libxmtp/blob/v2/bindings_swift/src/lib.rs#L136

nmalzieu commented 1 year ago

@richardhuaaa yes I saw, thanks a lot for this! "most disconnects should recover near instantly" => well, not if you have a bad connection!

The grpc keepaliv task is great to recover streaming fast when connection comes back - but my goal here is to also handle the moment when the connection doesn't come back just now - I need my users to know thei're currently not receiving messages and adapt the UI.

For the subscription_closed error => well I didn't see it before and don't know how the React Native SDK can catch it easily, but I'm happy to test it out - @nplasterer what do you think?