Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for persistent mobile connections on Android?

I'm considering using a persistent connection to a "cloud service" from an Android app. This would run all the time in a background service (or something like that).

I'm thinking of using web sockets or XMPP to keep the connection, basically looking for a light weight connection that won't drain battery. I want to be able to push notifications in real time to this connection, so periodic polling is not desired. I am aware of C2DM and other commercial solutions, but am looking to roll my own. This is why a web socket (or other light weight connection) is what I'm investigating. So if I go this route, what are some best practices I should be aware of?

I'm thinking of stuff like:

  1. how to prevent the battery from draining,
  2. How to handle IP address changes, etc?
like image 961
Paul Fryer Avatar asked Jun 21 '12 16:06

Paul Fryer


2 Answers

This might not be the answer you are looking for but I think you may want to rethink your architecture.

Things you can expect out of a mobile platform

  • Your IP address to change randomly
  • Your physical internet connection to be lost randomly
  • The OS to decide your not doing anything useful and killing your process
  • The connection type changing randomly (from WIFI to 4G to 3G to edge) and thus your IP to change

Basically your app needs to be able to handle a loss of connection, because its almost guaranteed to happen.

That being said, it is totally doable depending on your definition of real-time. If your willing to continually check that there is still a viable connection, you could keep any delays down to the minutes range. But this will drain the battery and there is not much you can do about it.

like image 147
8bitwide Avatar answered Sep 28 '22 04:09

8bitwide


Some things just don't go well together. That is "push notifications in real time" and "prevent battery draining". You sure have to make compromises here.

I can only recommend to try some Android Apps that use XMPP to get a feeling how they handle persistent connections, IP address changes and battery consumption. If they are open-source you can also view the code and learn from it. Yaxim, Project MAXS and Beem to name a few. Maybe you shoud also have a look at XEP-0286: XMPP on Mobile Devices

That said, are you sure that you want to reinvent the wheel when Google offers you C2DM? Which is optimized for this use case. I think that it has some delay, so it's no where "real-time". But again, either you will end up with an solution that tries aggressivly to establish a persisent connection and drains the battery, or you will have to live with some kind of delay (~ 0-30 min).

like image 34
Flow Avatar answered Sep 28 '22 03:09

Flow