How to keep web socket connection active when android app is closed

11,636

I have been working on a similar problem the past couple of weeks. This is what I have done:

  1. I am using AutoBahn Android for WebSocket implementation. I suspect the workings should be somewhat similar in any other implemention.
  2. There are basically 3 calls to implement the websocket handler:
    1. onOpen()
    2. onReceiveMessage()
    3. onClose()
  3. And 3 calls that you use in a websocket connection:
    1. connect()
    2. disconnect()
    3. sendMessage()
  4. Now this is where it gets tricky! You have to keep the connection alive. The first solution that comes to mind is an obvious Service. But a Service in itself won't be such a great way because it would be working on the UI thread of your application. Most of the time it would do its thing without disturbing you or causing you any harm. However, when you do have to open your application's User Interface, (I have an interface, and its important to me), it would cause you no end of grievance! And anyways, I couldn't seem to implement it in a very polished manner (by that I mean I used to lose the connection many times).
  5. So I extended an AsyncTask and in the doInBackground() method I called a function called mWSConnectionStart() which basically asked the WebSocketConnection to connect and do its thing. In the meanwhile, I postUpdate() from the AsyncTask in case I need to build a Notification.

This has been working well for me, and I hope this method works for you as well.

PS: A problem that I have been unable to solve is that of high CPU utilization. May you fare better!

PSS: UPDATE: I just saw that on my device, Whatsapp (which I have been benchmarking my apps performance against) has a higher CPU utilization (battery) than my app! Maybe it has something to do with WebSockets.

EDIT: I would really recommend using GCM instead of WebSocketConnections. We are planning to move towards GCM ourselves. Reason is that it doesn't require much server-side maintenance, plus it automates a lot of our tasks.

Share:
11,636
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    I'm looking to make an app that sends a message to a websocket to enable notifications for when a particular parameter changes inside a computer. The parameter changes in the computer, travels through the websocket, and goes to my application. This part is correct, right?

    How do I receive these websocket notifications even when my app is closed? This is what background data is, right? Should this be simple? My application doesn't need to send messages to the web socket when it's closed. All it needs to do is receive messages when it's closed. Ideally, it would say 'Tell me when parameter changes!' on installation, then stay connected forever and ever while it's installed on the device.

    Also, the web socket is hosted by a third party, so I don't have access to any server settings it might offer.

    EDIT: I feel like i'm missing an important concept in all of this. How do messaging systems receive push notifications when they're off? Through a web socket, right? So even when a messaging app is closed, it receives data?