Card 07/ 10

ComparisonDifficulty: Advanced2 min

WebSocket vs SSE vs WebRTC on the Wire

Each sends a message differently and fails differently, which decides which one a network will let through.

WebSocket, server-sent events and WebRTC differ on the wire in ways that decide where each works: SSE is plain HTTP, WebSocket is HTTP that turns into something else, and WebRTC leaves HTTP and TCP behind for UDP. The further a protocol moves from ordinary HTTP, the more proxies, load balancers and firewalls it has to get past.

The three protocols across the four questions, and through the infrastructure in between
QuestionWebSocketServer-sent eventsWebRTC
How it startsGET with Upgrade, answered by 101GET answered with text/event-streamOffer and answer by signalling, then ICE checks
Who may sendBoth sidesServer onlyBoth peers, directly
One message on the wireBinary frame, masked from the clientText lines ending in an empty lineSRTP or SCTP inside DTLS, on UDP
After a dropApplication reconnects and catches upBrowser reconnects with Last-Event-IDICE restart through signalling
Through a proxy or load balancerNeeds upgrade support and long idle timeoutsWorks if responses are not bufferedMedia bypasses them; may need TURN through a firewall
Data it carriesText or binaryUTF-8 text onlyAudio, video, text or binary

WebSocket through infrastructure. Every proxy and load balancer on the path must understand the Upgrade request and pass the connection through afterwards. Most current ones do, some only when configured. All of them still count the connection as idle when no frames pass.

SSE through infrastructure. It is an HTTP response, so anything that carries HTTP carries it. The usual fault is a proxy that collects the response before forwarding it, which holds every event until the buffer fills. Sending Cache-Control: no-cache, and disabling buffering for that route, avoids it.

WebRTC through infrastructure. Media does not pass through the web servers at all, so they never see the load. The obstacles are NATs and firewalls instead, which is what TURN is for.

The rule. Choose by the conversation first: server to many readers, SSE; both directions through your servers, WebSocket; device to device with media, WebRTC. Then check the path, because a protocol the network does not carry well is the wrong one whatever it was designed for.