[Chat App] Architecture - Network
Network
For chat system, we need to support real-time communication between users.
There are 2 main ways to support real-time communication:
- Client-Server Model (WebSocket)
- Peer-to-Peer (WebRTC)
Client-Server Model (WebSocket)

The client server model is that we use a central server (or multiple servers) to handle all connections and collaborative behaviors.
Pros and Cons
Features | Rating | Description |
---|---|---|
Centralized source of truth | 🟢 | All users have the same document from single source |
Scalability | 🟢 | Event hard to load balancing for Servers, comparing to peer-to-peer, only Server need to handle all connections |
Implementation | 🟢 | Comparing to peer-to-peer, the concept is simpler |
Single point of failure | 🔴 | Once server is down, all users cannot collaborate |
Cost | 🔴 | We need a long live server to handle all connections, which is hard to maintain |
Speed | 🔴 | Comparing to peer-to-peer, we need to go through server, which increase a network path |
Peer-to-Peer (WebRTC)

The peer-to-peer model is that only rely on clients themselves to handle all connections and collaborative behaviors.
Pros and Cons
Features | Rating | Description |
---|---|---|
Single point of failure | 🟢 | Even if any client is down, the other clients can still collaborate |
Cost | 🟢 | We don't need a long live server to handle all connections, only rely on clients themselves |
Speed | 🟢 | We can directly communicate with each client |
Centralized source of truth | 🔴 | There is no centralized source of truth, each client is the authority |
Scalability | 🔴 | When clients increase, every client have to handle more connections, which become a complex mesh |
Implementation | 🔴 | The concept and implementation of peer-to-peer is way more complex than client-server model |
Network Conclusion : Which one to use?
Here are the pros and cons of each model:
Features | 🏆 Client-Server Model | Peer-to-Peer Model |
---|---|---|
Single point of failure | 🔴 | 🟢 |
Cost | 🔴 | 🟢 |
Speed | 🔴 | 🟢 |
Centralized source of truth ⭐️⭐️⭐️ | 🟢 | 🔴 |
Scalability | 🟢 | 🔴 |
Implementation | 🟢 | 🔴 |
Since what chat system requires is correctness, which can be achieved by centralized source of truth on server. And Client-Server Model is easier to scale, 🏆 Client-Server Model will be our choice.
Server vs Serverless architecture

About the server architecture, there are 2 main types:
- Server:Build and maintain your own server
- Serverless:Use a third-party server (e.g. AWS, GCP, Azure) to handle the server
And here are the pros and cons of each type:
Features | 🏆 Server | Serverless |
---|---|---|
Long live protocols support ⭐️⭐️⭐️ | 🟢 | 🟡 |
Cost | 🔴 | 🟢 |
Scalability | 🟢 | 🔴 |
Control | 🟢 | 🔴 |
Since we need a long live server to have a better WebSocket support to execute real time communication, even Server Send Events to push new created room and last message to clients, which Serverless is not supported, 🏆 Server will be our choice.
Reference
- GreatFrontend - Google Docs
- Hussein Nasser - Backend Communication Design Patterns - WebRTC