WebSockets vs. WebRTC for browser multiplayer games
· 3 min read
Start with WebSockets for almost every browser multiplayer game—turn-based games, co-op RPGs, IO games, and even most casual 2D/3D action games. Only add WebRTC DataChannels (configured as unreliable/unordered UDP) when you are building a fast-paced twitch action or physics game where packet loss causes noticeable TCP stalls.
How they differ under the hood
| Property | WebSockets (wss://) | WebRTC DataChannels (unreliable) |
|---|---|---|
| Underlying transport | TCP | UDP (SCTP over DTLS/ICE) |
| Packet delivery | Ordered & guaranteed | Optional unordered / no retransmits |
| Head-of-line blocking | Yes (one dropped packet stalls newer updates) | No (stale position updates are simply skipped) |
| Setup complexity | Low (one URL + JSON/binary messages) | High (SDP signaling, ICE candidates, UDP ports / TURN) |
| Corporate/school firewalls | Almost always works over port 443 | Can fail without TURN relay fallback |
When WebSockets are the right choice
WebSockets run over standard HTTPS (port 443), take ten lines of code to set up, and work reliably behind strict Wi-Fi networks and proxies.
On clean broadband connections (0–1% packet loss), WebSocket latency is virtually identical to UDP. Combine a 20 Hz server tick rate with binary or compact JSON snapshots and client-side entity interpolation, and most indie multiplayer games feel smooth over WebSockets alone.
Good fits for WebSockets:
- Turn-based strategy, card, and board games
- Top-down co-op, survival, and roguelike games
- Social/party games and chat-heavy lobbies
- Any early prototype where shipping quickly matters more than squeezing out 15 ms under packet loss
When WebRTC DataChannels are worth the complexity
On lossy Wi-Fi or cellular networks (2–5%+ packet loss), TCP's reliability becomes a liability: if state snapshot #40 drops, TCP holds snapshots #41, #42, and #43 in the buffer until #40 is retransmitted—even though #40 is already obsolete. Players see a sudden freeze followed by a rubber-band snap.
An unreliable, unordered WebRTC DataChannel ({ ordered: false, maxRetransmits: 0 }) drops lost movement snapshots cleanly and processes the next fresh snapshot immediately.
Good fits for WebRTC DataChannels:
- Fast-paced FPS, arena shooters, and twitch fighting/racing games
- Server-authoritative physics games running 30–60 Hz state sync
Note that WebRTC does not have to mean peer-to-peer. Client-to-server WebRTC (using Go's pion/webrtc, Rust's webrtc-rs, or Node bindings) gives you UDP-like delivery while keeping your game server fully authoritative and cheat-resistant.
A practical hybrid pattern
Many production browser games use both:
- WebSocket for reliable events that must never be dropped (lobby join, chat, inventory changes, match start/end, and WebRTC signaling).
- Unreliable WebRTC DataChannel strictly for high-frequency movement inputs and world-state snapshots—falling back cleanly to the WebSocket if a player's firewall blocks UDP.
On Pita, [servers.central] and per-match [servers.instance] run over TLS WebSockets (wss://) in a fast sandboxed WASM/JS runtime with built-in SQLite, encrypted connect tokens, and millisecond wakeups—covering turn-based, co-op, party, and most 2D/3D browser games without TURN or ICE setup. If your game specifically requires unreliable WebRTC UDP datagrams, pair a WebRTC-capable container host (such as Fly.io) with your static web client.
FAQ
What about WebTransport?
WebTransport (HTTP/3 / QUIC datagrams) is designed to replace the painful WebRTC SDP/ICE handshake for client-server UDP games. Browser support is strong in Chrome and Firefox, though Safari support has lagged—making WebSocket + optional WebRTC fallback still the safest cross-browser choice today.
Should I send JSON or binary messages over WebSockets?
Start with JSON while iterating so you and your coding agent can inspect traffic in browser DevTools immediately. Switch high-frequency snapshot messages to ArrayBuffer / DataView or MessagePack only after profiling shows bandwidth or serialization is actually a bottleneck.