Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep alive socket connection in android 7.0 and above?

I am developing multiplayer game using Socket.io library. it Works Well. But, in android 7.0 and above, system automatically suspend all network work when my app is in background. (And I must need to keep alive my socket connection). I research about it as described here. but, i can't understand. So, Please provide solution for that.

like image 321
Abhay Koradiya Avatar asked Feb 27 '18 19:02

Abhay Koradiya


1 Answers

Unfortunately there's bad news and some good news for you on this.

Bad:

Since android marshmallow and above, there's a concept of a doze mode. If the device stays put for some time (can't confirm the duration for this and not disclosed by google), the device will go into doze mode and will suspend all network activity. There will be short maintenance windows where in you will be able to do syncs and stuff. Small workaround, do not target 23+ apis, i say small because i have observed this to not work on some phones. Another way to potentially bypass this would be to whitelist your app from battery restrictions but according to google guidelines, i don't think your app will qualify for that.

Worse news is that start from API 26, background services will also get suspended completely when app is totally backgrounded and has no visible component (a notification or a foreground service etc...). So oreo will be worse.

Good:

You might not really want to constantly keep the socket open. Instead opt for bursts of syncs. I personally have a job run every 30 - 60 mins or so to try and sync up.

You can leverage the JobScheduler apis and it will automatically handle the doze modes and stuff and you can make them run periodically when there is internet connection. While the job is running, you can connect to your server, do your thing and shut the socket. This is what google wants and is pushing all devs towards.

UPDATE 19-Apr-2021 WorkManager is the new and the best way to deal with doze mode and background limit restrictions.

Another alternative would be to have a foreground service with an active notification displayed which would constantly be connected via your socket. This will be visible to the user and it will not only annoy them that you are constantly using their data, it can also be bad for the battery. Alternative to this again is using the job scheduler to schedule and run a foreground service periodically so as to be transparent while also syncing your data every once in a while. The latter approach is what WhatsApp does, they have a job running which syncs all incoming messages with a foreground service once in a while.

In Short:

You will not be able to keep it alive always. You can try doing it in bursts using one of the methods that i described and know currently (maybe there are other alternatives that i don't know, i have tested these and they work) You will have to compromise, sorry.

like image 147
Kushan Avatar answered Oct 13 '22 01:10

Kushan