30

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?

2

6 Answers 6

23

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

12
  • 2
    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
    Commented Apr 19, 2013 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. :-) Commented Apr 20, 2013 at 3:26
  • 4
    Nat will not allow step 8, even if mobile is on static ip, which will never be the case. the possible ways are either Xmpp or long polling http
    – user37120
    Commented Jul 6, 2013 at 15:09
  • 1
    It does have the required permission. It's listed as "receive data from Internet" in the Play Store. See android.stackexchange.com/a/61794
    – user5244
    Commented Mar 14, 2015 at 18:59
  • 3
    Unfortunately, incoming TCP connections are next to impossible on mobile devices. The overwhelming majority of them do not have public IP addresses, and even if they do (e.g. for IPv6 capable operators), incoming connections are usually filtered. Most applications today either use Google's push services or roll their own persistent client to server socket connection (most notably Facebook). WhatsApp seems to take a hybrid approach, in my experience: If GCM is available, the persistent socket is closed after being idle for a few minutes.
    – lxgr
    Commented Jan 23, 2016 at 16:16
9

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.

3
  • 2
    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
    Commented Jul 25, 2013 at 14:42
  • 2
    @DylanYaga is correct. See e.g. the last paragraph of this answer and Trebors answer here.
    – Izzy
    Commented Jul 25, 2013 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
    Commented Feb 17, 2014 at 16:30
2

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

3
  • 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
    Commented Feb 17, 2014 at 12:59
  • 2
    It does have the required permission. It's listed as "receive data from Internet" in the Play Store. See android.stackexchange.com/a/61794
    – user5244
    Commented Mar 14, 2015 at 18:59
  • 2
    From what I've seen, they use GCM if it is available. If it is unavailable for some reason (e.g. on a device without Play services or if filtered by the network), the socket is kept around indefinitely.
    – lxgr
    Commented Jan 23, 2016 at 16:18
0

Whatsapp like other apps is run as service.(You are always connected) Messages sent to you are stored on whatsapp server till 30 days, within which period your whatsapp should be active to receive and acknowledge receipt of the message. Individual accounts are stored with the user phone number @whatsapp.net

1
  • 1
    30 days. Please link source for that fact
    – beeshyams
    Commented Feb 6, 2016 at 2:10
0

I think it should be like this:

When whatsapp was new, it would have used PNS based solution, this PNS would just wakeup the device and once it wakes up, it can create a socket connection to whatsapp server.

After when whatsapp is popular now most devices its coming as native application and native application will not be killed. In my samsung s8 i cant uninstall whatsapp, power saving mode can not kill it

0

Google maintains connection with android and apns server maintains connection with apple devices, without gcm or fcm no third party can send push notification to android devices.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .