Websocket connection closed after few seconds the app in background

On Android, this happens when the app is in background, when native share popup or admob rewarded video showing.

Is there any way to avoid it? Could we adjust something to make it last longer? Now just around 7 seconds, that’s not enough for doing actions

2024-02-11 13:01:14.038 25626-12477/com.chungxa.kyhoang I/FA-Ads: Application backgrounded at: timestamp_millis: 1707631272034
2024-02-11 13:01:23.158 977-4271/? V/WindowManager: Changing focus from Window{f2c706f u0 android/com.android.internal.app.ChooserActivity} to Window{6bdfc0c u0 com.chungxa.kyhoang/com.dynamo.android.DefoldActivity} 
2024-02-11 13:01:23.267 25626-12478/com.chungxa.kyhoang V/glfw-android: handleCommand (main thread): APP_CMD_RESUME
2024-02-11 13:01:23.268 4593-26884/? I/[AirCmd]_RemoteSpenMainController: hasGeneralAction : packageName is com.chungxa.kyhoang, activityName is com.dynamo.android.DefoldActivity, hasActions is false
2024-02-11 13:01:23.268 25626-12478/com.chungxa.kyhoang V/glfw-android: handleCommand (main thread): APP_CMD_GAINED_FOCUS
2024-02-11 13:01:23.268 25626-12478/com.chungxa.kyhoang E/defold: ERROR:WEBSOCKET: Websocket poll error: WSLAY_ERR_CALLBACK_FAILURE
2024-02-11 13:01:23.276 25626-12478/com.chungxa.kyhoang E/defold: ERROR:WEBSOCKET: Failed to setup callback
2024-02-11 13:01:23.283 25626-12478/com.chungxa.kyhoang D/defold: DEBUG:SCRIPT: [Colyseus] websocket error

Not sure to be honest. It could be that Android closes the connection to conserve system resources? There might be some setting to tweak in the underlying Wslay library we use for websocket connections, but I haven’t found anything. Also, is it so bad to reconnect if the connection is lost?

1 Like

There are some situations we must reconnect such as when internet connection lost but doing that after viewing admob rewarded video is kind of bad for me, because after reconnecting, the app must reload serveral stuff to sync with server state and in this case, reconnect, reload stuff, reward player for viewing ad is not what I’m expecting.

Actually I’m remaking my app. The app using react native not having this issue. I guess there must be settings for Activity or manifest to keep connection alive but I’m not familiar with native code.