How does WhatsApp work?

WhatsApp is a messenger that aims to replicate the SMS experience, but avoid the expense of SMS. Part of what makes SMS popular is that it rides on top of the phone numbers that you can also use to make a phone call. So you can exchange a phone number with someone, and then you have a choice of reaching that person via a telephone call, or an SMS text message.

WhatsApp expands on that idea so that you can now also reach a person via WhatsApp if you have their phone number. This phone number matching done by WhatsApp is not some optional feature — it’s a core aspect of using WhatsApp, as there is no other way of finding people than having their phone number.Once you have found someone to talk to on WhatsApp, the mechanism of chatting is pretty straightforward.The simplest phrase for WhatsApp is that it is a “store and forward” system.

This happens in a couple of places. The WhatsApp client will store any message you send, and keep trying to send it to the WhatsApp servers. his behavior varies somewhat by platform, as some mobile platforms do not allow arbitrary code execution, and this restriction can be visible to the user at times – this is most obvious on the iPhone and Windows Phone platforms, as apps have very restricted access to the CPU when not in the foreground. On other platforms, like Android, BlackBerry, BlackBerry-10, Symbian and Nokia S40, the OS allows background execution, so the user experience is substantially nicer.

This “store and forward” also happens on the WhatsApp chat servers. When a WhatsApp mobile app connects to the chat servers, the store-and-forward process occurs in both directions. The client sends any pending messages in its message store to the server, while the server sends all the accumulated messages destined for that client to the client. This process is done on an acknowledgement basis. This means messages are sent repeatedly until the other side acknowledges receipt of the message.

One difference between the server and the client for “store and forward” is that the server drops messages once they are acknowledged – there is no long term storage. Whereas the client will update the UI to double-check status once the ack. arrives, but will keep the message. In the WhatsApp framework, the client is the canonical message store, and the server is a router with temporary storage. This is in contrast to the more typical “store everything forever” strategy seen by other large communication providers operating today.

The lifecycle of a message from ‘A’ to ‘B’ is thus: -user A sends a message by hitting ‘send’ button. A’s client code connects to the chat server. A’s client sends the message body to the chat server. the chat server sends an ‘ack’ back to the client code so that the message isn’t sent again in the future.

This puts the message in ‘single check’ in the UI. Now the message is owned by the server. If we are lucky, client B is already online, and the message can be pushed down the existing connection B has established. If client ‘B’ is not currently online, then the server has a decision tree of actions to take to try to get B to connect. Typically a push notification is delivered to client B.

Some platforms will just display that notification directly to the user, and wait for user action to start the app. Others allow the push notification to be delivered directly to the WhatsApp application, in the background. Background delivery is again, a much nicer experience for the user, as WhatsApp can then setup a chat connection in the background, fetch the messages, then notify the user.

This means the user won’t have to wait for message delivery when they open the application, as the messages are already inserted into the message database. No matter how B’s client code is activated, once it connects to the server and receives A’s message, it sends back an ‘ack’ to the server. This relieves the server from the duty of delivering that message in the future. This ‘ack’ is routed all the way back to A’s client, and the message is then in ‘double check’ in the UI for A, indicating that the delivery process is complete.

Source: Whatsapp