WebRTC vs. WebSocket

Both WebRTC and WebSocket enable real-time, bidirectional communication over networks.

The key difference is that WebRTC combines ICE-based connection establishment with UDP-based encrypted data transport, while WebSocket is a messaging protocol typically built on TCP.

Let’s understand WebRTC by comparing it with the more familiar WebSocket.

WebSocket

WebSocket is a communication protocol at the application layer of the TCP/IP model.

HTTP was originally designed around a request-response model, where a client sends a request and a server returns a response. Applications such as chat systems traditionally relied on techniques like polling, where the client repeatedly asks the server for updates.

WebSocket provides a more efficient way to achieve bidirectional communication.

polling or websocket

A typical WebSocket connection using wss:// is established as follows:

  1. TCP three-way handshake
  2. TLS handshake
  3. HTTP 101 Switching Protocols
  4. WebSocket messaging begins

The client requests a protocol upgrade using an HTTP/1.1 request:

GET /chat HTTP/1.1
Host: example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: ...
Sec-WebSocket-Version: 13

Once the server responds with 101 Switching Protocols, WebSocket communication begins over the existing TCP connection.

From that point on, data is exchanged as WebSocket frames rather than HTTP requests and responses.

Alternative connection establishment mechanisms have also been standardized for HTTP/2 and HTTP/3.

WebRTC

Now, let’s look at WebRTC (Web Real-Time Communication).

Unlike WebSocket, which defines a single communication protocol, WebRTC combines multiple mechanisms for connection establishment, path discovery, encryption, and data transport.

Connection Establishment

WebRTC can be understood through three main functions.

FunctionTechnologiesPurpose
SignalingSDP, Offer/AnswerExchange connection information and communication parameters
ConnectivityICE, STUN, TURNNAT traversal and connection establishment
Data TransportSCTP, DTLS, SRTPData transfer and encryption

webrtc connection setup

① Signaling

Peers exchange communication parameters using SDP (Session Description Protocol), typically through WebSocket or HTTP.

WebRTC does not specify which protocol must be used for signaling.

② ICE

ICE (Interactive Connectivity Establishment) checks possible combinations of IP addresses and ports to determine a working communication path.

  • STUN: Discovers external addresses assigned through NAT and performs connectivity checks.
  • TURN: Relays data when a direct connection cannot be established.
  • ICE: Gathers and checks connection candidates, then selects a working communication path.

③ Secure Communication

Once a communication path has been established, DTLS performs a handshake to establish secure communication.

If a direct connection succeeds, the peers communicate through P2P. Otherwise, communication can be relayed through a TURN server.

Data Transport

WebRTC uses different protocols depending on the type of data being transmitted.

DataChannel (Arbitrary Data)

SCTP and DTLS are used to transmit text and binary data.

SCTP handles message delivery, retransmission, and ordering, while DTLS provides encryption and authentication.

These protocols typically operate over UDP/IP.

Media (Audio and Video)

SRTP is used to transmit audio and video.

SRTP provides encryption and authentication for media packets, typically transported over UDP/IP.

webrtc data transport

Both DataChannel and Media use communication paths selected by ICE.

Conclusion

The main differences between WebSocket and WebRTC lie in how they establish communication paths and which protocols they use.

WebSocket enables bidirectional communication over a TCP connection. WebRTC, on the other hand, uses ICE to discover and establish communication paths, combining multiple protocols for encryption and data transport.