Best practice for persistent mobile connections on Android?
android, real-time, websocket, xmpp
Solution
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.
Problem
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: - how to prevent the battery from draining, - How to handle IP address changes, etc?