Introduction
...
For a chat service, the choice of the network protocols is important : HTTP connection could be a good option on the server-side, but the problem occurs on the client-side. There are 3 techniques to simulate a server-initiated connection: polling, long polling and WebSockets.
See polling & long polling in the page References & Glossary for chat system.
WebSockets is the most common solution for sending asynchronous updates from server to client. WebSockets (WS) is used for both sender and receiver sides.
...
High-level shows 3 major categories : stateless services, stateful services and third-party integration. High-level architecture is already scalable because a single server design is a deal breaker (single point of failure).
...
Chat servers facilitate message sending/receiving.
Presence servers manage online/offline status.
API Servers handle user login, signup, change profil, etc.
Notification servers send push notifications.
KV Store to store chat history : when offline, user see all previous chat history. See the page References & Glossary for chat system - Storage.
Step 3 - Design deep dive
...
Each device maintains a variable called “cur_max_message_id” which keeps tracks of the latest message ID on the device.
...
On the recipient side, a recipient can receive messages from multiple users (see diagram below).
...
Online Presence
This indicator is an essential feature of many chat applications.
...
When user disconnects from internet, the persistent connection between the client and server is lost. We cannot update statuts on every disconnect/reconnect, it’s creating a poor user experience.
Implementation of “heartbeat event” : sending an event every x seconds.
...
Presence servers use a publish-subscribe model in which each friend pair maintains a channel.
...
Step 4 - Pros & Cons
Pros: Decoupled architecture, real-time communication.
...