Before Switching to P2P, I Measured Whether WebRTC Would Even Connect
I'm building Clipboard Share, a browser clipboard for moving files between devices on the same network: https://www.clipboardapp.org. I wanted to make transfers faster by using WebRTC. Before changing anything, I shipped
I'm building Clipboard Share, a browser clipboard for moving files between devices on the same network: https://www.clipboardapp.org. I wanted to make transfers faster by using WebRTC. Before changing anything, I shipped a small probe that only measures whether a direct connection works.
One thing up front: file transfer is still not P2P. Files go to Cloudflare R2 and the receiver downloads from R2. The only place in the codebase that creates a DataChannel is the probe, and it sends throwaway test data.
Why I looked at P2P
The current flow is "upload everything, then download." With a multi-GB file, the receiver waits for the whole upload even if the sender is sitting next to them. The goal in docs/p2p-decision.md:
When the other device is already in the room, send the file over WebRTC directly and cut the wait of "download only after the upload finishes."
Why measure first
Direct connections fail in some network setups, and a device that joins later can't receive anything over a connection that no longer exists. So the doc states a rule before any data came in:
Files are always stored in R2. P2P is only a faster path for devices that were already connected at send time. Devices that join later, or refresh, get the file from R2 as they do today.
It also fixes the pass criteria up front (dated 2026-10-24): C1 connection rate >= 85%, C2 transfer completion >= 97% with zero mismatches, C3 P2P median speed bucket at least one step above R2 uploads, C4 at least 30 uploads of 8 MB+ in 7 days. Miss any one and the next step doesn't happen. If it passes, the next step is a "shadow mode": keep uploading to R2 as usual, and have the receiver verify P2P data with SHA-256 per 1 MB, then discard it.
How the probe works
Nothing changes on screen. When a device joins a room that already has another device, it tries to connect and reports the result as a GA4 p2p_probe event.
1. The server only relays signaling
Offers, answers and ICE candidates travel over a Socket.IO event, p2p:signal. The handler forwards {to, data} as {from, data}, and only to a socket in the same IP room:
const ipRoomId = socket.ipRoomId;
if (!ipRoomId || to === socket.id) return;
const target = io.sockets.sockets.get(to) as ExtendedSocket | undefined;
if (!target || target.ipRoomId !== ipRoomId || !target.rooms.has(ipRoomId)) return;
target.emit('p2p:signal', { from: socket.id, data });
The only thing it checks about data is its size (16 KB cap after JSON serialization). There's also a per-socket token bucket (burst 40, 20/sec).
2. Classify how it connected
Once the channel opens, I read the selected candidate pair from getStats(). There's no TURN server, so a relay candidate counts as a failure.
export function classifyCandidateTypes(localType, remoteType) {
const valid = ['host', 'srflx', 'prflx', 'relay']
if (!valid.includes(localType) || !valid.includes(remoteType)) return 'failed'
if (localType === 'relay' || remoteType === 'relay') return 'failed'
if (localType === 'srflx' || remoteType === 'srflx') return 'direct_nat'
return 'direct_lan' // host/prflx
}
3. Send test data and verify it
A connected channel can still be useless, so the probe actually sends data: 8 MB on a LAN path, 2 MB through NAT. Each 16 KB chunk starts with its index; the receiver only counts bytes and checks order, then drops the data.
export const PROBE_BYTES_LAN = 8 * 1024 * 1024
export const PROBE_BYTES_NAT = 2 * 1024 * 1024
// receiver: replies ack:<bytes>:<ok|bad> on the end signal
recvBytes += d?.byteLength ?? 0
if (readChunkIndex(d) !== expectIdx) orderOk = false
expectIdx++
The sender marks it complete only if the acked byte count matches and the order flag is ok; otherwise mismatch. The transfer timeout scales with size: max(8 s, bytes / 1.5 Mbit/s), capped at 30 s. If the connection itself isn't up in 8 seconds, it's failed.
4. Once per session, and it can't break the app
export const SESSION_FLAG = 'p2p_probe_done'
export function isProbeEnabled(envValue = import.meta.env?.VITE_P2P_PROBE_ENABLED) {
return envValue !== 'false'
}
One probe per session (sessionStorage), every exception is swallowed with a console.warn, and VITE_P2P_PROBE_ENABLED=false turns it off. The unit tests cover timeout to failed, mismatch on bad bytes or order, stalled when no ack arrives, and closed when the channel drops mid-transfer.
For comparison I also log real R2 upload speed: a successful upload of 8 MB or more sends an upload_perf event (upload_mbps, upload_path, retries, resumed, and so on). P2P only matters if it beats that.
Results
Between Oct 24 and Nov 25, GA4 recorded 672 p2p_probe events from 428 users. 177 of them came in the last 7 days, so the probe is still running.
| What | Count |
|---|---|
Probes that got an answer from the peer (got_answer=true) |
533 |
Direct connection direct_lan / direct_nat
|
394 / 39 |
Connection failed
|
238 |
Test transfer complete / stalled / other (closed, error, complete_legacy) |
395 / 30 / 3 |
Data mismatch
|
0 |
Checked against the thresholds I wrote down first:
- C1 connection rate: 433 direct Γ· 533 answered β 81%. Below the 85% bar.
- C2 transfer completion: 395
completeout of 428 test transfers β 92%. Below the 97% bar. There were zero mismatches, so data never arrived wrong; most of the gap isstalled(no reply to the end signal). - C3 speed and C4 demand: the
upload_perfevent I use as the R2 baseline shows 0 events in GA4 for this period. I haven't found out why yet, so I can't judge speed or demand.
The rule was "if any check misses, don't move to the next step." With these numbers, shadow mode doesn't start.
What's still weak
- One probe per session with 2-8 MB of data. It may not match a multi-GB transfer.
- Results are GA4 aggregates, so I can't drill into which device pairs fail.
- It only tries the first peer in the room (
peers[0]), so a room with many devices is measured as one pair. - STUN only, no TURN. Networks that block direct connections simply count as failures.
- There is no P2P file transfer code today. The criteria are written down, but the shadow-mode step and anything after it don't exist yet.
I built this with Claude Code. If you try it, I'd like to hear what's rough. There's a feedback link at the bottom of the site: https://www.clipboardapp.org
Originally published in Korean on velog.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.