How WhatsApp works
WhatsApp uses XMPP (eXtensible Messaging and Presence Protocol) to handle the message delivery system.
XMPP is mostly like HTTP where the client opens the socket with the XMPP server and keeps it open as long as the client is logged in. It’s not like the regular REST API where the client opens the socket send/receive the data and close the socket. The socket is opened as long as you are signed in. In case of WhatsApp that’s an eternity (not really, WhatsApp reconnects automatically if the connection terminates)
XMPP protocol has been used in various chat applications such as Google Talk, Facebook messenger, etc. I have been working on XMPP since last year and that’s when I got to know what this protocol could bring. It’s not limited to chat apps though. We’re developing an application of our own which is not a chat app.
As far as actual technology goes, WhatsApp uses a heavily customized version of Smack library on Android to build their client and uses customized eJabberd Server to handle the XMPP traffic. They might have different backend solution for handling the data though which might be on one of the cloud storage/computing network (I think it’s Heroku, no real idea though).
On iOS and other platforms, I suppose they might have developed their own libraries. Developing own libraries is not a lot of work, especially when you have customized needs and have a team of developers.
Anyways, if you are interested in learning the tech, you can read Oriely’s “XMPP: The Definitive Guide” and can visit The XMPP Standards Foundation
Assumption added by one of my colleagues, How WhatsApp is using XMPP
He gives one example, let says there are two users A and B, and user A send message to user B. User A has opened the WhatsApp and he sends a message, let’s says hello to user B and user B is not using WhatsApp. According to this User A is an active user and User B is not an active user, and rest of the flow I will convince with a diagram.
Now according to the above picture, when user B will open the message from the notification tray, at that time User B will become an active user and the XMPP will establish the dedicated connection. Like this only one instance of the socket will be live at a time. This will solve the mobile battery draining and internet usages problem. This approaches might have some loophole but that you will find and let me know through the comment section.