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.

Looks like currently dmSocket running on main thread (UI thread?) and when the thread put in background, paused… the socket connection is also dropped. If it’s the case, can we run it on a different thread or a background service?

I’m not sure what you mean with “dmSocket running on main thread”. It is a handle, it doesn’t run anything.
As for the connection, it’s done on a thread.

Also, not sure what using more threads would solve. All the app’s threads would be put to sleep in the background, not just the main thread.

1 Like

So it’s not related to threads but it can be solved by background services. After some researches, I see someone can keep the connection alive when app put in background.

“I have created a background service in which I am using the tcp socket connection. When the app is live either in background/foreground, the connection is working as expected.”
TCP Socket in BG service is terminated when device enters sleep mode. - Clover Community

You know, seeing other game online apps can keep connection alive for a long time when switching apps but my app can’t is uncomfortable

I propose youi open a ticket in the extension where you add any relevant information to help anyone implement it.

I’m not entirely convinced an android-only fix is ideal, as we have plenty of other platforms to support, and I’d preferably have a platform agnostic way .

Ok, I’ll open a ticket on github for this.

Just checked DefNet library, the TCP connection still keep alive when app in background (lost focus). Interesting…