Why I bothered learning WebRTC
It started on a game night when my friends and I had a deck of cards, plenty of time, and zero poker chips. I threw together a quick chip tracker, and over a few weeks it snowballed into PlayKit: poker chips, a chess clock, scorekeeping, tournaments, Undercover, and Imposter.
Passing one phone around the table gets old fast, so I wanted everyone to join on their own phone. That gave me an excuse to finally learn WebRTC. Most tutorials jump straight into video calls, which I didn’t need at all. I just wanted a clean way to send small game moves and table updates over a data channel between one host laptop (or phone) and everyone else at the table.
Before building this, I half-assumed “peer-to-peer” meant two browsers could just magically talk without a server. Turns out the first thing WebRTC teaches you is how much work goes into getting two devices to meet in the first place.
Getting two browsers to meet
When someone hosts a room in PlayKit, they get a join link to share with the table. Each friend who opens it connects to a tiny signaling service I put on Cloudflare, and the host opens a separate WebRTC connection to each guest.
How a phone joins a game
- Meet through signaling. Both the host and the joining phone open authenticated WebSockets to a Cloudflare Worker backed by a room Durable Object. This server has one job: pass setup notes back and forth.
- Swap an offer and an answer. The host creates an ordered data channel called
playkit-v1and sends an SDP offer describing how it wants to talk. The phone saves that as its remote description and sends back an SDP answer. - Trade possible network routes. Each browser collects ICE candidates (possible addresses it might be reachable on) and trickles them through signaling. One detail that tripped me up early: candidates can show up before the remote description is ready, so PlayKit queues them and applies them once it is.
- Test routes and open the channel. ICE tries candidate pairs until it finds a path that actually responds. Once the encrypted data channel opens, the phone can ask for a seat—though the host still has to approve it.
- Hand control to the host. From here on, the host checks every move and sends updated game snapshots back out. Just reaching the signaling server doesn’t put you in the game.
If you want the official walkthrough of this handshake, WebRTC’s peer-connection guide is the clearest reference I found.
What goes over which wire
Signaling (Cloudflare)
WebSocket to the Worker and room Durable Object
- SDP offer and answer
- ICE address candidates
- Invite check during join
- Basic room and peer metadata
Only for introductions
Data channel (direct)
Encrypted, ordered playkit-v1 channel between host and guest
- Seat requests
- Bets, folds, and other moves
- Full table snapshots, cards included
Everything about the actual game
# in the URL, and the Durable Object only keeps a SHA-256 hash of it.ICE, STUN, and TURN (the part that confused me most)
Every time I read about WebRTC before this project, these three acronyms blurred together. Seeing them next to actual routers finally made them click. A phone sitting on Wi-Fi or mobile data usually has a private local IP that nobody outside its router can reach, and firewalls love dropping unexpected traffic.
Getting past two routers
Example addresses- ICE
- Interactive Connectivity EstablishmentThe matchmaker. It gathers every possible address a device could use (local, STUN, or TURN), tests pairs between the two browsers, and picks one that actually works. ICE isn’t a server carrying your data—it’s the process that finds the route.
- STUN
- Session Traversal Utilities for NATA lightweight “what’s my public IP?” helper. Your browser pings a STUN server, and the server replies with the public IP and port your router showed it. That’s often enough for two devices to punch through and talk directly, though STUN never carries the actual game traffic.
- TURN
- Traversal Using Relays around NATThe fallback plan. When strict routers or corporate firewalls refuse a direct connection, a TURN server sits in the middle and relays the encrypted traffic for both sides. It makes connections way more reliable, at the cost of paying for and running relay bandwidth.
MDN’s page on WebRTC protocols goes deeper if you want the full spec breakdown.
Tradeoffs I made along the way
One host runs the table
When I first sketched this out, I wondered if every phone should connect to every other phone in a mesh. Five minutes of thinking about poker cured me of that. If two people tap a button at the same instant, who resolves the conflict? And with five friends at a table, a mesh means ten connections to babysit instead of four.
Why one host instead of a mesh
5 players around a tableInstead, PlayKit uses a simple star: every guest talks only to the host. Guests send requests (“I want to raise”), the host checks whether it’s valid, and the host broadcasts the new state. If the host closes their laptop, the room ends—there’s no automatic host migration. For a living-room game night, I’m totally fine with that tradeoff.
- Gain
- One place to check rules and settle conflicts, with far fewer connections to manage.
- Cost
- No host migration. If the host browser drops out for good, the session goes with it.
For poker specifically, the host checks three things on every incoming move: does this player own that seat, are we still on the revision they think we’re on, and is the move actually legal? If everything passes, the host saves the move locally first, then sends each phone only the slice of state it’s allowed to see. Undo history and duplicate-action tracking never leave the host.
What happens when a player taps Raise
Full snapshots instead of a replay log
Because a poker hand or a score sheet is tiny, I didn’t bother building a delta/replay protocol. PlayKit uses an ordered data channel and sends a full snapshot on every state revision. If a friend’s phone locks and misses two turns, I don’t have to figure out which deltas to replay—they just get the latest snapshot when they wake up.
I also made the buttons stay locked whenever a phone drops or reconnects. You can’t tap a blind bet while offline and hope it goes through later; your controls only turn back on after a fresh snapshot lands from the host. Once the data channel opens, the phone also repeats its hello once a second (up to a 30-second timeout) until the host answers, so a lost opening message doesn’t leave someone stuck on a spinner.
When a phone goes to sleep mid-hand
Time runs left to right- Gain
- No stale-screen mistakes or messy offline merge logic.
- Cost
- You wait a beat after reconnecting for the latest snapshot before your buttons come back.
Keeping the game off my signaling server
I wanted the Cloudflare side to know as little as possible about what’s happening at the table. Cards, bets, and snapshots only travel over the encrypted peer-to-peer channel, never through the Worker or Durable Object.
For invite links, I tucked the secret into the URL fragment (the part after #). Browsers don’t send the fragment to the web host on the initial page load, and the join page strips it out of the address bar as soon as it saves the credentials. The phone does still send that invite during the WebSocket signaling handshake so the room can verify it, and the Durable Object only stores a SHA-256 hash of it.
- Gain
- Game state never touches the signaling server, and raw invites aren't stored there.
- Cost
- The signaling server still checks the invite during auth and can see basic connection metadata.
What I still want to test
Most of my testing so far has been on my own laptop and Wi-Fi. There are a few real-world things I haven’t actually measured yet:
- Try physical phones across different Wi-Fi and mobile data combos to see where STUN-only breaks.
- Log which ICE candidate pairs actually win in practice (local vs. server-reflexive).
- Watch what happens when a phone sleeps for ten minutes mid-game and wakes back up.
- Price out a small TURN relay and decide if it’s worth running for a side project.
Read the code
Everything here matches PlayKit as I checked it on 20 October 2026 (commit 5822c97). If you want to poke around, docs/phone-sync.md lays out the authority and recovery rules, and src/sync/transport.ts has the actual WebRTC offer/answer flow, candidate queue, and data channel setup.
