How WhatsApp Handles "Last Seen" and "Online" Status
WhatsApp uses a Last Seen microservice to manage user online status. This service stores timestamp data indicating when each user was last active.
Overview
The Last Seen microservice is responsible for tracking the following:
- When a user opens WhatsApp
- When a user sends or receives a message
- When a user views a message
The Last Seen microservice updates the user's last active timestamp whenever one of these events occurs. This timestamp is then used to calculate the user's online status.
Updating Last Seen
When a user opens WhatsApp, the Last Seen microservice updates the user's last active timestamp to the current time. This means that the user will be shown as "online" to their contacts.
If a user sends or receives a message, the Last Seen microservice also updates the user's last active timestamp. However, the timestamp is not updated if the user only views a message.
Application vs User Activity
The Last Seen microservice must distinguish between two types of activities:
- Application activities: These are activities that are performed by the WhatsApp app itself, such as polling for new messages.
- User activities: These are activities that are performed by the user, such as sending a message.
For application activities, the last seen status is not updated. This is because application activities do not necessarily indicate that the user is actively using WhatsApp.
Only direct user actions, such as sending a message, will trigger a last seen time update.
Technical Flow
When User 1 sends a message to User 2, the following technical flow occurs:
- The message is sent to the Gateway Service. The Gateway Service is responsible for routing messages between users.
- The Gateway Service determines the Session Service that is responsible for User 2. The Session Service is responsible for managing the connection between User 1 and User 2.
- The Session Service sends the message to User 2.
- The Last Seen microservice updates User 1's last active timestamp.
