Server communication and scheduler stops after being in the background

Server communication and scheduler stops after being in the background

Hi there,

In my app I have communication with my server through both XMLHttpRequests and Websockets.

When the app is running in the background for a small amount of time and then I open it up again, it seems like all communication is stopped. The app itself loads properly, and I can switch scenes and anything, but my XMLHttpRequests and WebSocket messages are not sent, and I can’t listen for any messages from the server with my WebSocket.

Also, my scheduled functions are not executed while in background. But I need to do that, as I have a timer which should always be running, even if the app is not opened.

Does anyone know why that is and how I can fix this?

Best regards

Plz explain your use case of the timer which should always be running.

You can imagine it as a round in a multiplayer match.
Of course the server sends a message to all players when the match ends, but still I want to give a timer so the user can see how much time is left. And I don’t want to send a message from the server every second to keep the time up to date.

When you say app, you do mean a mobile app, specifically iOS, Android, or both? There are background permissions on mobile apps for running them in the background otherwise networking stops. When you reopen the app you might need to reconnect to websockets. Look into app background processes for iOS and Android if you are talking about mobile.

1 Like

Yes, I was talking about mobile. Thanks :slight_smile:

regarding the timer which needs to be running even when the game is background, I’d like suggest the following.

  1. when the game starts, you should get the remaining time form server side.
  2. then, run your timer function to update the remaining time on client/game side.
  3. when the game goes background, listen to EVENT_HIDE and save the remaining time and current time, lets call it HIDETIME.
  4. when the game is foreground again, listen to EVENT_SHOW and load the saved HIDETIME and calculate how long we were in the background, and adjust your remaining time using that value. At same time, you should reconnect to your socket here.

If you don’t mind calling server frequently, I’d suggest you better get the remaining time form server again at EVENT_SHOW rather than calculating from a saved state.

1 and 2 are already happening, 3 and 4 should do the trick, thank you :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.