Engineering Deep Dive: Pushing WebRTC to the Limit
Pushing WebRTC to the Limit
When we set out to build NowDrop, we knew we wanted to rely on WebRTC. It’s the industry standard for real-time communication, but most developers only use it for video calls. Using WebRTC Data Channels for massive file transfers presents an entirely different set of engineering challenges.
The Memory Leak Problem
If you try to send a 5GB file using a naive WebRTC implementation, your browser tab will instantly crash. Browsers attempt to read the entire file into memory before sending it over the network.
To solve this, NowDrop implements streaming chunking.
We read the file from the user's hard drive in tiny 64KB chunks using the new File System Access API. We only read the next chunk after the previous one has successfully transmitted. This keeps memory usage completely flat, regardless of file size.
Dynamic Backpressure
The next problem was backpressure. What happens if a sender on gigabit fiber optic is transferring a file to a receiver on a slow 3G cellular network?
If the sender sends data faster than the receiver can process it, the data packets back up in a queue, eventually overflowing the buffer and terminating the connection.
We implemented a watermark algorithm:
- If the buffered data crosses
16MB, we pause the sender. - We wait for an acknowledgment signal from the receiver.
- Once the buffer drops below
4MB, we resume transmission.
This guarantees a flawless transfer without dropped packets, even on terrible network conditions.
Looking Forward
We're currently experimenting with integrating the new WebTransport API to make transfers even faster in modern browsers. Stay tuned!