WebTransport
medium 30 minWebTransport gives a browser and a server one encrypted HTTP/3 session that carries many independent reliable streams and also datagrams that may be lost.
See it run
One HTTP/3 session: how it opens, how streams and datagrams differ, what a loss does, and what happens when the network changes.
Operations
Open a session
From the first packet to the first stream.
Step 1 of 5
QUIC and TLS together
The browser's first packet starts both the QUIC connection and the TLS 1.3 handshake.
Step 1: QUIC and TLS together
def accept(request, settings): if not settings.webtransport: return None # client may not even ask if request.origin not in ALLOWED: return 403 if not valid(request.token): return 401 return 200 # the session exists- Time
- About 2 round trips
- You must build
- Origin and token checks
- WebSocket over TLS 1.3
- about 3 round trips
Name the second round trip: the client needs the server's settings before CONNECT.
In the real world
The same webtransport doing real jobs. Pick one and step through it.
A server is replaced while players are connected.
Step 1 of 3
Drain
draining resolves in the browser
Server 1 sends the drain signal to its sessions. Players go on playing.
Step 1: Drain
Common mistakes
The bugs interviewers watch for, with the fix beside each.
Reading a stream as messages
criticalA read returns whatever bytes are there: half a message or three.
Wrongmsg = json.loads(stream.read())Rightsize = read_exact(stream, 4)msg = json.loads(read_exact(stream, size))Datagrams for what must arrive
criticalA lost match result is never sent again.
Wrongsend_datagram(match_result)Rightstream.write(frame(match_result))Accepting every Origin
criticalAny web page could open sessions with the visitor's network position.
Wrongcheck_origin = lambda r: TrueRightcheck_origin = lambda r: r.origin in ALLOWEDNo limit on the handshake
majorWhere UDP is blocked the connection hangs and the page with it.
Wrongt = await webtransport(url)Rightt = await wait_for(webtransport(url), 3)Applying old datagrams
majorPackets arrive out of order. A late one would move the player backwards.
Wrongposition = d.positionRightif d.seq > last_seq: last_seq, position = d.seq, d.position
Interview tips
How to explain it
- One HTTP/3 session.
- Independent reliable streams, plus lossy datagrams.
- A loss delays only its own stream.
Memory trick
- Boats must arrive, stones may sink.
- One jetty per stream.
- Fog: take the road.
Reach for it when
- Updates expire quickly.
- Unrelated data shares a connection.
- The browser talks to your server.
Be careful with
- Networks without UDP.
- Balancers that route by address.
- Datagram size and stream framing.