Welcome to the final lesson in our module on real-time and collaborative systems. In our previous sessions, we explored how to broadcast information to many users simultaneously, designing systems for presence and live comments using server-pushed events like WebSockets and SSE. Today, we shift our focus from one-to-many broadcasting to direct peer-to-peer (P2P) communication, a pattern that powers some of the most demanding real-time applications like video and voice chat.
This lesson addresses a frequent senior-level system design topic: how applications like Google Meet, WhatsApp, or Discord establish direct video calls between users. We will dissect the architecture of WebRTC (Web Real-Time Communication), the cornerstone technology for this capability. You will learn why two browsers can't simply connect on their own and understand the critical roles played by Signaling, STUN, and TURN servers in making this "magic" happen.
1. The Peer-to-Peer Challenge: Why Browsers Need a Chaperone
On the surface, a direct connection between two browsers seems simple. If your browser wants to send a video stream to my browser, why can't it just send the data directly to my IP address? The primary obstacle is a ubiquitous feature of modern networks: NAT (Network Address Translation).
Your experience designing and deploying services on AWS provides a great parallel. In a Virtual Private Cloud (VPC), your EC2 instances have private IP addresses (e.g., 10.0.1.x). To access the public internet, they route traffic through a NAT Gateway, which has a single public IP address. From the outside world's perspective, all traffic from your instances appears to come from that one public IP.
The same principle applies to home and corporate networks. Your laptop, phone, and other devices each have a private IP address assigned by your router (e.g., 192.168.1.5), but they all share a single public IP address provided by your ISP. This creates two problems for P2P communication:
- Discovery: Your browser doesn't inherently know its own public IP address. It only knows its private one, which is useless to anyone outside your local network.
- Reachability: Even if the other peer knew your public IP address, your router's firewall would likely block the unsolicited incoming connection, as it wouldn't know which internal device to forward the traffic to.
To understand this problem in more detail, let's turn to a great conceptual article on the topic.
How Video Calling Actually Works: WebRTC and the Architecture ...
This article provides a clear, high-level explanation of WebRTC and the problems it solves without getting bogged down in code.
Please read the introduction, What is WebRTC?, and the following section, The Connection Problem. Focus on how NAT acts as a "wall" that prevents direct communication between browsers.
Because of this "NAT wall," browsers need help to find and connect with each other. This is where a Signaling Server comes into play.
2. The Matchmaker: The Role of the Signaling Server and SDP
Think of the signaling server as a mutual friend introducing two people at a party. The friend doesn't participate in their conversation but makes the initial introduction so they can start talking directly. In the world of WebRTC, the signaling server is an out-of-band channel that peers use to exchange the metadata necessary to establish a connection.
Crucially, WebRTC does not define or provide a signaling implementation. It's up to you, the developer, to build this. You could use WebSockets (which we've discussed), a REST API, or any other mechanism that allows two clients to pass messages to each other through a central server. Your experience with NestJS and Node.js is directly applicable here; you could easily build a WebSocket-based signaling server to handle this message passing.
What messages are they passing? There are two main types:
-
Session Description Protocol (SDP): This is the what of the communication. An SDP message is a standardized text block describing a peer's capabilities and configuration. It answers questions like:
- What video/audio codecs do you support (e.g., VP9, H.264, Opus)?
- What resolutions are you offering?
- What security keys will we use for encryption?
This exchange happens via an Offer/Answer Model. The calling peer creates an SDP
offer, sends it through the signaling server to the callee. The callee receives the offer, generates a compatible SDPanswer, and sends it back. -
ICE Candidates: We'll cover these in the next section, but they represent the how of the communication—the potential network paths to connect.
The following diagram illustrates this initial handshake.

The key takeaway is that the signaling server is only for control messages. The actual audio and video media stream does not pass through it.
3. Finding the Path: ICE, STUN, and TURN
After the SDP offer/answer exchange, the peers have agreed on the media formats, but they still don't have a network path to send the data. This is solved by the ICE (Interactive Connectivity Establishment) framework, which uses a combination of STUN and TURN servers.
This entire process of discovery and negotiation is often called "NAT traversal."
Zoom System Design | WhatsApp / FB Video Calling System Design | System Design Interview Question
This video segment provides an excellent system design-level overview of how P2P connections are established, explaining the roles of Signaling, STUN, and TURN servers in a clear, practical way. The speaker frames these components as a "Signaling Service," a "Connector" (STUN), and a "Call Server" (TURN), which is great for an interview context.
Please watch the section from the P2P connection process. Pay close attention to how each server type contributes to solving the NAT traversal problem.
Let's break down the components mentioned in the video:
STUN Server (The "Connector")
A STUN (Session Traversal Utilities for NAT) server has one simple job. A client sends it a request, and the STUN server responds, "Here is the public IP address and port from which I received your request."
By contacting a STUN server, a browser behind a NAT can learn its own public-facing IP address. It then gathers this and other potential addresses (like its private IP) into a list of ICE candidates. These candidates are the possible network paths that other peers can try to connect to. Each browser sends its list of candidates to the other peer using the signaling server.
The Connectivity Check
Once both peers have each other's ICE candidates, the WebRTC engine in the browser begins a connectivity check. It tries sending data packets (a process called "hole punching") between all possible pairs of local and remote candidates. The first pair that successfully gets through establishes the P2P connection. In most cases (>80-90% for consumer networks), this is successful, and media can flow directly.
TURN Server (The "Call Server")
Sometimes, even with STUN, a direct connection is impossible. This often happens with "Symmetric NATs" or restrictive corporate firewalls that block P2P traffic. When all direct connectivity checks fail, WebRTC falls back to a TURN (Traversal Using Relays around NAT) server.
A TURN server is a media relay. Instead of communicating directly, both peers send their encrypted media streams to the TURN server, which then forwards the streams to the other peer.

Using a TURN server ensures a connection can almost always be established, but it comes with significant trade-offs:
- Increased Latency: The media has to take an extra hop through the relay server.
- Increased Cost: The TURN server must have enough bandwidth to handle all the relayed video streams, which can be very expensive to operate at scale.
A senior engineer designing such a system would focus heavily on maximizing the P2P connection success rate to minimize reliance on costly TURN servers.
4. The Complete Picture: A Step-by-Step Flow
Let's consolidate everything into the sequence of events for a one-on-one video call:
- Initiation: Alice clicks "call Bob." Alice's browser gets her local audio/video stream.
- Offer Generation: Alice's browser creates an
RTCPeerConnectionobject and generates an SDPofferdescribing her media. - Signaling - Offer: Alice's browser sends the SDP
offerto the signaling server, addressed to Bob. - ICE Gathering (Alice): Concurrently, Alice's browser contacts a STUN server, discovers her public IP, and begins gathering ICE candidates. As candidates are found, they are sent to Bob via the signaling server.
- Signaling - Forwarding: The signaling server finds Bob is online and forwards Alice's SDP
offerand her ICE candidates to him. - Answer Generation: Bob's browser receives the offer. It creates its own
RTCPeerConnectionand generates an SDPanswer. - ICE Gathering (Bob): Bob's browser also contacts a STUN server and gathers its own ICE candidates.
- Signaling - Answer: Bob's browser sends his SDP
answerand his ICE candidates back to Alice via the signaling server. - Connection Establishment: Both browsers now have each other's SDP and a list of ICE candidates. They perform connectivity checks to establish a direct P2P data channel.
- Media Flow: If a direct path is found, encrypted media flows directly over UDP. If not, the connection is established via a TURN server. The signaling server is no longer involved in the media path.
This entire dance of offers, answers, and candidates is orchestrated by the signaling server but executed by the WebRTC APIs in the browser.
Conclusion
In this lesson, we demystified the process of establishing a P2P video call in the browser. You now understand the architectural components and the flow of information required, which is precisely the level of detail expected in a senior frontend system design interview.
Key Takeaways:
- WebRTC is the browser-native technology for real-time P2P communication, but it cannot establish connections on its own due to NAT and firewalls.
- A Signaling Server is a developer-built "matchmaker" that passes metadata—SDP Offers/Answers (the what) and ICE Candidates (the how)—between peers before a connection is made.
- STUN servers are simple utilities that help a peer discover its own public IP address.
- TURN servers are expensive but necessary fallback relays that forward media when a direct P2P connection is not possible.
This lesson concludes our module on real-time systems. In the next module, we will pivot to an equally critical area of large-scale frontend development: Performance Monitoring & Observability. We'll move from building features to measuring their real-world impact, learning how to track Core Web Vitals, design effective error reporting systems, and gain deep insights into our application's health in production.