Reputation: 9537
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:
Upvotes: 6
Views: 6817
Reputation: 24083
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).
Upvotes: 1
Reputation: 2071
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
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.
Upvotes: 10