ably-js: Unable to Reconnect Suspended Connection upon App Reactivation in React Native

Expected Behavior:

  • Suspended Ably connection should automatically reconnect when the app is reactivated.

Actual Behavior:

  • Suspended connection remains suspended and does not reconnect, even after explicitly calling connection.connect().

Usually when apps goes on background iOS/Android kills socket connections after some time, as expected. During this time, ably connection could go suspended, which is fine. However, when the app comes back active, the expected behavior is that the previously suspended connection should reconnect. But this doesn’t happen, even after explicitly calling connection.connect().

To reproduce:

  • Integrate Ably on a React Native app. Make sure to include a reconnection strategy in your code.
  • On a real device, run and then minimize the app, or lock the screen for about 20 - 30 minutes (enough time after connection suspension).
  • Reopen the app and notice that your ably connection never reconnects even if you called connection.connect();

Here are my reconnection strategies for when ably connection is suspended on the background.

// Reconnection strategy when Ably connection is suspended
ably.connection.on('suspended', () => {
  setAblySocketConnected(false);
  setIsAblySuspended(true);
  console.warn('Ably socket suspended!');
  console.log('Attempting reconnection...');
  ably.connect();
});

// Attempt Ably reconnection when app becomes active
const AppState = useAppState();
useEffect(() => {
  if (AppState === 'active' && isAblySuspended) {
    AblyClient.current?.connection.connect();
  }
}, [AppState, isAblySuspended]);

Environment:

  • Ably SDK version: 1.2.44
  • React Native version: 0.71.8
  • OS: Android, iOS

┆Issue is synchronized with this Jira Bug by Unito

About this issue

  • Original URL
  • State: closed
  • Created 9 months ago
  • Comments: 20 (6 by maintainers)

Most upvoted comments

@mattsrobot glad to hear it 😃

FWIW I have been able to reproduce the issue described by @danielrhodes where authCallback doesn’t work correctly on react-native after the initial token has expired, so you may encounter some issues still. Hopefully we should have this fixed soon, will let you know in this thread when it’s resolved.

@Dajust I had a lot of trouble using the callback - it didn’t call the callback when the token expired, only when Ably was initialized. I instead had to implement it like below where Ably does the auth request.

I haven’t tried fiddling with the timeouts very much.

{
    autoConnect: true,
    echoMessages: true, // Using this for receipts
    disconnectedRetryTimeout: TIMEOUT, //currently at 5000
    suspendedRetryTimeout: TIMEOUT,
    recover: (lastConnectionDetails, cb) => {
       //some logic I am using to determine if I should try to recover the connection
    },
    transportParams: {
      heartbeatInterval: TIMEOUT,
    },
    clientId: <YOUR-CLIENT-id>,
    queryTime: true, // Relying on local time might cause drift, invalidating login.
    authMethod: 'POST',
    authUrl: <YOUR-AUTH-UURL>,
    authHeaders: {
     <YOUR-AUTH-HEADERS>
    },
  };