How does WhatsApp work
Siegfried Meurer, (more)Loading.
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 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 at a couple of places. The WhatsApp client will store any message you send, and keep trying to send it to the WhatsApp servers. This 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 it mesage 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 acknowlegement basis. This means messages are sent repeatedly until the other side acknowledges receipt of the message.
The lifecycle of a message from to is thus:
user A sends a message by hitting button.
A client code connects to the chat server
A client sends the message body to the chat server
the chat server sends an back to the client code so that the message isn sent again in the future. This puts the message in 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 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 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 client code is activated, once it connects to the server and receives A message, it sends back an to the server. This relieves the server from the duty of delivering that message in the future. This is routed all the way back to A client, and the message is then in check in the UI for A, indicating that the delivery process is complete.