Requirements:

Recap: Why WebSocket for bidirectional communication?

High Level Design

Source: ByteByteGo https://bytebytego.com/guides/how-do-we-design-a-chat-application-like-whatsapp-facebook-messenger-or-discord/

Source: ByteByteGo https://bytebytego.com/guides/how-do-we-design-a-chat-application-like-whatsapp-facebook-messenger-or-discord/

Service Discovery

Role: Recommend the best chat server for a client based on geographical location, server capacity, etc. (e.g., Apache Zookeeper)

Once a user has login via HTTP services, the service will return a stable, suitable WS chat service for user via Service Discovery:

{
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
  "ws_urls": {
    "chat": "wss://chat-sg1.mychatapp.com/connect",
    "presence": "wss://presence-sg1.mychatapp.com/connect"
  }
}

Presence Server (Online/Offline Indicators)

Presence servers are responsible for managing online status and communicating with clients through WebSocket.

Source: ByteByteGo https://bytebytego.com/courses/system-design-interview/design-a-chat-system

Source: ByteByteGo https://bytebytego.com/courses/system-design-interview/design-a-chat-system