SitePoint16 min readtutorialintermediate
Streaming Encrypted Files P2P with WebRTC DataChannels and WebCrypto in TypeScript
Summary
The tutorial shows how to build a browser‑only P2P file transfer using WebRTC DataChannels, the Streams API, and WebCrypto to encrypt 64 KB chunks on the fly, while applying backpressure via bufferedAmountLowThreshold. This prevents memory blow‑outs and ensures integrity with per‑chunk SHA‑256 verification.
- Apply backpressure by setting RTCDataChannel.bufferedAmountLowThreshold and pausing sends until the bufferedamountlow event fires, avoiding OOM crashes.
- Slice files into exact 64 KB chunks with a TransformStream to match SCTP limits and enable fine‑grained flow control.
- Encrypt each chunk with AES‑GCM, prefixing a unique 12‑byte IV (index + random) before sending over the DataChannel.
- Verify each received chunk against a pre‑computed SHA‑256 manifest to detect tampering immediately.
Frontend engineers building browser‑based P2P file‑transfer apps should care because it demonstrates a memory‑safe, end‑to‑end encrypted streaming architecture using standard web APIs.
6/10





