Android - How is WhatsApp able to receive messages when not in use?
At first, I thought this was most likely an instance of Android cloud to device messaging, but it can't be: WhatsApp doesn't declare the necessary permissions, and it works in Eclair (Android 2.1), while cloud to device messaging (and its replacement, Google Cloud Messaging) require at least Froyo (Android 2.2).
However, it is most likely some sort of push notification service. It's impossible to say exactly how it's implemented without access to WhatsApp's source code.
The part about it being able to receive messages when not in use is relatively easy to explain, though. The app almost certainly has a service which runs in the background to check in with the server periodically (or receive the server push, or whatever it does). When you receive a message, the service pops up a notification.
As for the question as to how the server could do a push, here's a possibility (again, I can't verify without source code):
- WhatsApp starts and opens two sockets: One to listen on and one to send a message to the server.
- WhatsApps starts listening on the first socket.
- WhatsApp sends a message containing your phone number and the port of the listening socket to the server and waits for an acknowledgement.
- The server records the phone and port numbers in the message and the IP address that the message came from.
- The server sends an acknowledgement to the app.
- The app receives the acknowledgement and closes the message socket.
- A message with your phone number as the destination comes into the server.
- The server uses the IP address and port number it has associated with your phone number and uses that information to push the message to your phone.
For this to work, the app would need to monitor the state of the network connection and reset the listening socket / resend the registration message when necessary (if the IP address changes, for example).
I am certainly sure that WhatsApp does NOT open any listening ports. most ISPs block incoming requests, that would not work.
WhatsApp has a service. Basically that means that technically you never quit WhatsApp. So, the way you receive messages while you're "not running" WhatsApp is the exact same way you receive them when you're running it.
The client, in this case WhatsApp, connects to the server. The client maintains that connection. When the server has something new to say, it sends it to the client via that connection, And that's all.
Your first guess is correct.
WhatsApp has no true log out mechanism, see this.
WhatsApp does not have a logout mechanism. The application is designed to always be connected so that you receive your messages quickly, even when you are not actively using your phone. This is similar to how SMS works and allows WhatsApp messages to be delivered almost instantly.
Their FAQ suggests they use GCM. Check the answer to "Are messages still taking too long to reach your phone?"