React-native-firebase: Android Slow To Reconnect After Device Goes To Sleep

Created on 19 Jul 2017  路  7Comments  路  Source: invertase/react-native-firebase

Hey guys, Awesome work on this library!

Running into one weird issue. Android only but happening on multiple Android devices that are running a number of apps. The repro is something like:

  1. Open App
  2. Let phone sleep for five minutes
  3. Wake phone
  4. Push to a firebase Ref (e.g. firebaseDatabase.ref('rooms/roomId/messages').push(newMessage);)

The new message is synced to the localCache but NOT to my firebase DB. This is normal, it's working as if it were offline. I would expect it to sync shortly after but it's taking over 60 seconds to sync.

Running adb logcat whenever I try to call .push I see the message Calling JS function after bridge has been destroyed

Have you guys ever encountered anything like this / have any idea what's going on? Thanks!

Environment

  1. Target Platform (e.g. iOS, Android): Android
  2. Development Operating System (e.g. macOS Sierra, Windows 10):
  3. Build tools (Xcode or Android Studio version, iOS or Android SDK version, if relevant):
  4. React Native version (e.g. 0.45.1): 0.44.3
  5. RNFirebase Version (e.g. 2.0.2): 1.1.0
Database

Most helpful comment

Do you guys know if there's any kind of reason why android just won't connect under certain network circumstances? We've had it with a few users where they just go offline and don't go back online, over a few versions now (currently 3.1.0), and it only happens on android. Is anyone else experiencing this?

All 7 comments

Hey,

Is this on a production build? You you try adding firebase.database().goOnline(); when the app wakes to see if it helps?

Hey @Ehesp thanks for the reply. This is on production and debug builds.

I can give that a shot

I'd suspect this is down to the Firebase SDK detecting both when the phone moves to the background (plus network connection state). In one of our apps we have:

componentDidMount() {
  AppState.addEventListener('change', this.handleAppStateChange);
}
handleAppStateChange() {
    if (state === 'active' && this.isConnected) {
      firebase.database().goOnline();
      this.props.loadConfig();
    } else if (state === 'background') {
      firebase.database().goOffline();
    }
}

@Ehesp Thanks for the tip. Yeah it seems goOnline doesn't work unless goOffline is called first. I'm working on a messaging app so it's very inconvenient (and a bit buggy) to disconnect from firebase every time the app is backgrounded.

Wondering if you have any other ideas?

@jemise111 you could perhaps try something similar to @Ehesp 's example but instead do something like:

componentDidMount() {
    AppState.addEventListener('change', this.handleAppStateChange);
}
handleAppStateChange() {
    // this.isConnected being the devices network connectivity status
    if (state === 'active' && this.isConnected) {
      // force the db back into an online state
      firebase.database().goOffline();
      // on the next event loop
      setImmediate(() => firebase.database().goOnline());
    } else if (state === 'background') {
      // do nothing
    }
}

Probably not the nicest solution but i'm not sure if the native firebase sdk's even provide any functionality to persist the connection in the background unless you write some native code to create a background service. See firebase reply on this SO post.

Ideally your messages should also route through FCM, that way when the app is in the background you still get message notifications/events. The messaging implementation in RNFirebase supports this.

Having said that, calling keepSynced(true) is supposed to keep the socket connection open as long as possible until the OS itself terminates the connections, so might also be work trying that, e.g.:

firebase.database().ref('rooms/roomId/messages').keepSynced(true);

@Salakar we tried something similar to your latest suggestion and in a few days of testing we haven't seen the issue again, so thank you!

Going to close this and may reach out again if the problem pops back up

Do you guys know if there's any kind of reason why android just won't connect under certain network circumstances? We've had it with a few users where they just go offline and don't go back online, over a few versions now (currently 3.1.0), and it only happens on android. Is anyone else experiencing this?

Was this page helpful?
0 / 5 - 0 ratings