Take the 2-minute tour ×
Android Enthusiasts Stack Exchange is a question and answer site for enthusiasts and power users of the Android operating system. It's 100% free, no registration required.

I'm curious about how WhatsApp is able to receive messages when not in use.

I mean, I understand that WhatsApp works using the Internet connection rather than the cellphone network. Imagine that I haven't touched my phone in hours. At that moment, a friend sends me a message on WhatsApp. I get it immediately. Does that mean that WhatsApp is keeping the Internet connection connected at all times? Does that mean that WhatsApp acts as a server, opening ports on the phone? Or maybe a long-running HTTP request?

share|improve this question
    
Also see whatsapp.com/faq/en/s60/20910578 –  Pacerier yesterday

3 Answers 3

up vote 9 down vote accepted

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):

  1. WhatsApp starts and opens two sockets: One to listen on and one to send a message to the server.
  2. WhatsApps starts listening on the first socket.
  3. WhatsApp sends a message containing your phone number and the port of the listening socket to the server and waits for an acknowledgement.
  4. The server records the phone and port numbers in the message and the IP address that the message came from.
  5. The server sends an acknowledgement to the app.
  6. The app receives the acknowledgement and closes the message socket.
  7. A message with your phone number as the destination comes into the server.
  8. 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).

share|improve this answer
1  
Does Whatsapp depend on the playstore app? Quoting from the Wikipedia link: It requires devices running Android 2.2 or higher that also have the Market application installed. So it might not be exactly that, but certainly is based on the same principles. You might want to include some excerpt from the article's 2nd paragraph, though, to make your answer more valuable (and receive more upvotes ;) –  Izzy Apr 19 '13 at 19:14
    
You're quite right, @Izzy, I should have added more details. It was hard to do while I was sitting at Subway writing on my SGS3, though. I've updated the answer now. :-) –  Trebor Rude Apr 20 '13 at 3:26
    
You were pointing in the right direction :) After your edit, it looks perfect to me, so +1 :) –  Izzy Apr 20 '13 at 12:46
    
How could the server do a push? That's the crux of the question. –  Ram Rachum Apr 20 '13 at 17:52
    
I've updated my answer to include a possibility for how the server could accomplish a push. –  Trebor Rude Apr 20 '13 at 20:14

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.

share|improve this answer
1  
Your link is for Nokia - I was unable to find a similar question for Android on their site. Are you sure that the same applies to Android? This Question from their Android FAQ indicates that it utilizes a PUSH architecture (under the section "Are messages still taking too long to reach your phone?") - rather than a constantly connected server/client. –  Dylan Yaga Jul 25 '13 at 14:42
1  
@DylanYaga is correct. See e.g. the last paragraph of this answer and Trebors answer here. –  Izzy Jul 25 '13 at 14:48
    
This seems the most logical. Many times these services are called push services, but I can't imagine that the client would open a port and in fact act as a local server. That would certainly cause security issues. –  SPRBRN Feb 17 at 16:30

Their FAQ suggests they use GCM. Check the answer to "Are messages still taking too long to reach your phone?"

share|improve this answer
    
You're right that the FAQ suggests that, but as Trebor Rude points out, the app doesn't have the necessary permission, so they can't be using GCM itself. –  Dan Hulme Feb 17 at 12:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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