3

I am in the process of designing a chat application with cordova for android devices. I have been researching and have come to the conclusion that there are two ways to go about this that could work.

  1. Websockets. Using websockets the client application could send a message to a webserver, the webserver could lookup the ip address of the recipient within a database and send the message to the recipient. This will require much more logic on the server but will eliminate the need for constant polling. This method would also require a constant loop on the client that would detect network changes and send the updated ip address to the webserver so that the webserver would know where to send the message.
  2. Constant polling. Have client application constantly poll the web server for updates. This would be the easiest method as server logic would be limited. Also there would be no need for logic in the application to handle the case when users switch networks. However the common consensus is to stay away from designs that utilize constant polling as it is a massive drain on device battery life.

My question is then: Does the issue of battery life for constant polling warrant the extra logic/time to implement a solution with websockets?

  • 1
    I think doing it using web sockets will be less onerous than you think it is. It's clearly the better way to do it. – Robert Harvey Aug 6 '15 at 17:36
  • You shouldn't have to poll for ip address changes. You only need to check when the network starts. (or restarts.) – Steven Burnap Aug 6 '15 at 18:48
3

Web Sockets are the better choice

The problem with constant polling is that, much like sending small bursts of data (which it pretty much amounts to), your device goes into an 'idle' state that it has to 'wake up' from in between polls. There is a high cost associated with making the initial connection and this cost is repeated for every poll you perform. And this adds up. For a chat application where you would need to poll every few seconds to keep it responsive you'd drain the battery in no time.

See also this answer on android.SE

Web sockets are not that hard anyway. There are plenty of libraries out there that can help you, and take care of the issues you list for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.