Understanding WhatsApp Architecture

Written by lakhwinder | Published 2022/08/12
Tech Story Tags: software-development | software-architecture | whatsapp | software-engineering | programming | insant-messaging-solutions | architecture | application

TLDRWhatsApp is one of the most widely used cross-platform messaging applications. The advantage of a system like WhatsApp is that the communication and transmission of information is practically in real-time**. We must think that it must include a series of functionalities so that it is minimally optimal, like WhatsApp. The two most popular types of chats are [Facebook Messenger], which is responsible for saving all messages indefinitely, and the other is WhatsApp, which stores them until they are delivered. It must be able to support the playback of Youtube videos, audio and video MP4, 3GP, MKV, AVI, and MOV.via the TL;DR App

Understanding the Architecture of an Instant Messaging Service like WhatsApp

WhatsApp is one of the most widely used cross-platform messaging applications. On it, information is sent immediately, which is why more and more websites include it as another service to facilitate communication with users.

What will you read in this piece?

  • FUNCTIONALITY

  • ARCHITECTURE SYSTEM

  • WHATSAPP FRONT END

  • WHATSAPP BACK END

The two most popular types of chats are Facebook Messenger, which is responsible for saving all messages indefinitely, and the other is WhatsApp, which stores them until they are delivered. Once read and delivered by the target user, they are removed from the server and disappear.

Functionality

The advantage of a system like WhatsApp is that the communication and transmission of information is practically in real-time.

For this reason, when it comes to implementing and discovering the technical scenario of the architecture of an instant messaging system, we must think that it must include a series of functionalities so that it is minimally optimal, like the many of WhatsApp:

  • One-to-one chats
  • Sending messages offline
  • The groups
  • Must allow up to 256 participants to participate
  • Video chats and group video chats
  • voice messages
  • It must be able to share images, video, and voice. By the way, the videos cannot weigh more than 16 MG, or have a duration between 90 seconds and 3 minutes.
  • It must indicate if the messages are read or the last viewing time by the user
  • The sent, delivered, plus read receipt option
  • Submitted files are limited to 100 MB
  • It has to be able to support the playback of Youtube videos, audio and video MP4, 3GP, MKV, AVI, and MOV
  • Reply to private messages
  • Ability to copy, save or archive messages
  • You can block messages
  • Take photos and record conversations, share locations, and access the contact list.
  • Link to other devices
  • Configure the information, security, and privacy of the user

Architecture System

Source:Medium

Source: Medium

Profile Database

First, user information such as status, profile picture, profile ID, and contact details must be collected and saved.

It is essential to save in the cloud (for example in a similar service or like the one offered by Amazon’s S3 ) the images of the users, each one of them with their links.

Profile Service (API)

It is allowed to publish, add, update or even retrieve the information of each user.

The advantage of a system like WhatsApp is that the communication and transmission of information is practically in real time . For this reason, when it comes to implementing and discovering the technical scenario of the architecture of an instant messaging system, we must think that it must include a series of functionalities so that it is minimally optimal.

ChatServer

Mapping Database

It is also important to have a clear communication architecture. Users A and B use bidirectional communication to communicate over the network, so you need to know each other’s IP addresses.

This process ends with a server, users, load balancing (which distributes traffic between multiple servers and avoids network saturation), and the database.

  • It is convenient to replace the HTTP protocol, where when each user sends a request or signal to the server and a response is received, closes the connection, with the WebSocket protocol, which, although it is a bit more complicated, is a server that works very well for applications in real-time and that will always maintain a continuous connection between active users.

Up to 65K ports will be open during the process. The socket controller will provide information about the web sockets connected to each user. Redis is used because it stores and is able to provide both which web socket controller a user is connected to, and whether to connect to another controller on disconnection.

Messenger Service. The API will obtain a repository of all message information through various filters such as user identification, message identification, delivery status, etc. For example, the open source big data solution Cassandra developed by Facebook can be integrated, which is capable of storing large amounts of information.

From here, several scenarios can happen and develop:

  • The user A or sender who sends the info is connected to the server, but the recipient (user B) is not ; in this case the message is stored in the database and when the recipient connects, that message is sent again.

  • If the sender (A) is not connected to the server; then that message is stored (for example in a database like SQLite), and when it connects, it will be sent to the server.

  • However, if both are connected (A and B). The message is not stored but sent, it will travel through the pass-through server without staying there.

Data mapping: Every time a user sends a message, a new process is generated in which the information is stored. The chat server will spawn a new process for user A and with user B if he is online. The server identifies the receiver’s name through the PID (Process ID) and is immediately sent. The same process is repeated in reverse when user B is going to reply to user A.

Group chat service How does it work?

  • Unlike one-to-one chats, keep in mind that for each group that is created in WhatsApp, a new GroupID will be generated, with an assignment of all the users that are included. When a message is sent to the group. The chat service identifies the recipient, which in this case will pass to the group service to locate the same group ID and who sent messages to all the user IDs in the list.

  • In this case, the web socket servers are not going to keep track of the group, but of the active users of the group. So when someone launches and sends a message to the group. The server contacts the message service and these will be stored in Cassandra. They are then saved to Kafka (which is a data transmitter), which will connect to the group message server and send them all. The server will connect to the group service, locate the list of group members, and deliver the message to each member individually.

  • The group service has control over all the information of the group, each one of the members, the user ID, group ID, until the time of creation, etc. data to be saved and stored in a MySQL database.

Last seen messages

The service offered by instant messaging and that allows you to check the “last seen” option uses a specific database that stores this information.

Technically every time a user opens WhatsApp, the server contacts this option to send the time details.

Messages with files or multimedia

  • Once the instant messaging service detects the type of file that has been sent. It is stored in the cloud (like the S3 we talked about earlier). The links are going to go to a SQL database with mapping to user details. In this case, HTTP can be used to transfer the messages.
  • The images, files, or video that have been sent will be compressed and encrypted, and this encrypted content will reach the receiver.

WhatsApp front end

Each of the versions of WhatsApp is made with a different language:

Android version: with Java

iOS: with Swift

Windows Phone: C#

The web version: with JavaScript/HTML/CSS

The Mac desktop version: Swift/Objective-C

The PC desktop version: C/C#/Java

WhatsApp back end

Erlang is the primary language that has been used for WhatsApp.FreeBSD for the operating systemEjabberd is your application server XMPP

BEAM is your Erlang-based virtual machine

Mnesia is your Erlang-based database

YAWS is your multimedia web server

Lead Image credit


Also published here.


Written by lakhwinder | I’m a freelance professional with over 5 years' experience writing.
Published by HackerNoon on 2022/08/12