TL;DR
🚀 Live Web App (Free, Zero Signup): air-stream.pages.dev
⭐ GitHub Repo (MIT): github.com/QuantumTechCoder/AirStream
📖 The Series So Far:
The Hidden Trap of WebRTC File Transfers
If you read any WebRTC file transfer tutorial online, you'll see people celebrate Backpressure Control:
"Just monitor dataChannel.bufferedAmount! If the buffer exceeds 1MB, pause file slicing until onbufferedamountlow triggers. Boom, zero RAM crashes on the sender!"
And that works brilliantly... for the sender.
The sender simply reads a 64KB slice from disk via file.slice(), ships it down the socket, and lets the garbage collector discard it. The sender's RAM never exceeds 30MB, even when transferring a 100GB disk image.
But what happens on the receiving device?
Almost every WebRTC app on GitHub handles incoming chunks like this:
// The Classic WebRTC Receiver Trap
const incomingChunks: Uint8Array[] = [];
dataChannel.onmessage = (event) => {
incomingChunks.push(new Uint8Array(event.data)); // Storing raw chunks in RAM!
if (receivedBytes >= totalBytes) {
// Attempting to assemble 10GB into a single JavaScript Blob
const fileBlob = new Blob(incomingChunks);
const downloadUrl = URL.createObjectURL(fileBlob);
}
};
On a developer workstation with 32GB or 64GB of DDR5 RAM, sending a 4GB or 5GB video across a gigabit local Wi-Fi router works without a hitch. You test it, high-five yourself, and deploy to production.
Then a user opens the app on their iPhone (Safari) or Android phone (Chrome) to receive a 10GB video.
At 1.4GB received, the browser tab freezes. Two seconds later:
"A problem repeatedly occurred with this webpage." (Safari)
"Aw, Snap! Out of Memory." (Chrome)
The tab was assassinated by the mobile OS kernel.
The Anatomy of the Mobile Memory Ceiling
Why do mobile browsers crash so aggressively on large transfers?
-
Per-Tab Heap Quotas: On iOS WebKit (which powers every browser on iOS, including Chrome and Firefox for iOS), the operating system enforces an aggressive per-process memory limit. Depending on the device model, tabs that cross ~1.2GB to 1.8GB of RAM are instantly terminated via
SIGKILL. Android Chrome enforces similar per-tab process memory caps. -
The "Blob Assembly Spike": When you hold 80,000 distinct
Uint8Arraychunk references in a JavaScript array, the V8/JavaScriptCore garbage collector cannot free a single byte. Worse, when you callnew Blob(incomingChunks), the browser momentarily doubles the memory overhead while constructing the internal blob descriptor structure. -
Double Buffering: Creating
URL.createObjectURL(blob)keeps the entire file pinned in browser heap storage until the tab is closed orURL.revokeObjectURL()is called.
If AirStream's motto is "Stream Gigabytes Browser-to-Browser with Zero Limits", crashing on 10GB transfers to phones was completely unacceptable.
We needed a way to stream incoming 64KB WebRTC packets directly to the device's storage disk in real time, completely bypassing the JavaScript heap.
The Progressive Direct-to-Disk Architecture
To solve this across all major platforms, I engineered a 3-tier progressive storage engine:
┌────────────────────────────────────────────────────────┐
│ Incoming 64KB WebRTC Chunk │
└───────────────────────────┬────────────────────────────┘
│
Does browser support Disk Streaming?
│
┌───────────────┴───────────────┐
Yes (Chrome/Edge) Yes (iOS Safari, Firefox)
│ │
▼ ▼
┌──────────────────────┐ ┌──────────────────────┐
│ Tier 1: FSA Engine │ │ Tier 2: OPFS Sandbox │
│ showSaveFilePicker() │ │ navigator.storage │
│ -> createWritable() │ │ -> createWritable() │
└──────────┬───────────┘ └──────────┬───────────┘
│ │
▼ ▼
Saved directly to OS disk Streamed into device sandbox
RAM Usage: < 20MB RAM Usage: < 25MB
Tier 1: File System Access API (Chromium / Desktop)
In Chromium browsers (Chrome, Edge, Opera), the W3C File System Access API allows web apps to stream bytes directly into a file handle chosen by the user:
// When the incoming transfer starts:
const fileHandle = await window.showSaveFilePicker({
suggestedName: meta.name,
});
const writable = await fileHandle.createWritable();
// As each 64KB WebRTC chunk arrives:
await writable.write(chunkPayload);
// When transfer completes:
await writable.close();
// Done! The file is already on the user's hard drive!
The Benefit:
-
Zero RAM Consumption: As soon as
writable.write(chunkPayload)executes, the bytes are flushed to the OS filesystem cache. The 64KB buffer is immediately garbage collected. - Zero Download Delay: The file doesn't need to be "downloaded" at the end. Once the progress bar hits 100%, the file is already sitting in their Downloads or Documents folder, completely written!
Tier 2: Origin Private File System (OPFS — Safari & Mobile)
The File System Access API is fantastic, but what about iOS Safari, macOS Safari, and Firefox where showSaveFilePicker is not supported?
Enter the Origin Private File System (OPFS).
OPFS provides a private, sandboxed, highly-optimized filesystem accessible via navigator.storage.getDirectory(). It is fully supported in iOS Safari 15.2+, Safari 17+, Chrome, and Firefox!
Crucially: OPFS requires zero permission prompts or user gestures.
Here is how AirStream automatically routes chunks to OPFS when receiving files $\ge$ 50MB:
// src/core/transfer/DiskStreamWriter.ts
export async function createDiskStreamWriter(
fileId: string,
_fileName: string,
fileType: string,
fileSize: number,
preferredFsaHandle?: any
): Promise<DiskWriter> {
// If user on Chrome picked a destination folder
if (preferredFsaHandle?.createWritable) {
const writable = await preferredFsaHandle.createWritable();
return {
storageMode: 'fsa',
write: (chunk) => writable.write(chunk),
close: async () => {
await writable.close();
return { savedToDisk: true };
},
abort: () => writable.abort(),
};
}
// Automatic OPFS Sandbox on iOS Safari & modern browsers (files >= 50MB)
if (isOpfsSupported() && fileSize >= 50 * 1024 * 1024) {
const root = await navigator.storage.getDirectory();
const tempFileName = `airstream_${fileId}.tmp`;
const fileHandle = await root.getFileHandle(tempFileName, { create: true });
if (typeof (fileHandle as any).createWritable === 'function') {
const writable = await (fileHandle as any).createWritable();
return {
storageMode: 'opfs',
write: (chunk) => writable.write(chunk),
close: async () => {
await writable.close();
// Access the disk-backed file handle
const file = await fileHandle.getFile();
// createObjectURL on an OPFS File does NOT allocate RAM!
const downloadUrl = URL.createObjectURL(file);
return { downloadUrl, savedToDisk: false };
},
abort: async () => {
await writable.abort().catch(() => {});
await root.removeEntry(tempFileName).catch(() => {});
},
};
}
}
// Tier 3: In-Memory Blob Fallback (for small files < 50MB)
const chunks: Uint8Array[] = [];
return {
storageMode: 'ram',
write: async (chunk) => { chunks.push(chunk); },
close: async () => {
const blob = new Blob(chunks, { type: fileType });
chunks.length = 0;
return { downloadUrl: URL.createObjectURL(blob), savedToDisk: false };
},
abort: async () => { chunks.length = 0; },
};
}
Why OPFS is a Game Changer for Mobile:
When createObjectURL(file) is called on an OPFS-backed File object, the browser does not load the file into JavaScript RAM. The blob URL points directly to the sandbox file on disk. When the user taps "Save", the native mobile browser streams the bytes directly into the iOS Files app or Android Downloads folder!
Concurrency Challenge: The Sequential Write Queue
WebRTC DataChannels can receive packets at rates of 20MB/s to 100MB/s+ on local Wi-Fi. That means incoming chunks can arrive every 1 to 2 milliseconds.
File system writes (writable.write()) are asynchronous. If you call writable.write() concurrently before the previous chunk has flushed, the browser will throw an error:
"InvalidStateError: A write operation is already in progress."
To guarantee that chunks are written strictly in order without locking the thread or dropping packets, I implemented a Sequential Promise Queue:
// src/core/transfer/FileReceiver.ts
public handleChunk(packetBuffer: ArrayBuffer): void {
// Decode framed header (chunk index, fileId)
const payloadBytes = new Uint8Array(packetBuffer, 8 + fileIdLen);
const active = this.activeTransfers.get(fileId);
if (!active) return;
// Chain writes sequentially onto a promise queue
active.writeQueue = active.writeQueue
.then(async () => {
const writer = active.writer || (active.writer = await active.writerPromise);
await writer.write(payloadBytes);
})
.catch((err) => {
console.error('Error streaming chunk to disk:', err);
});
// Track progress metrics...
}
When the sender finishes, finalizeTransfer() simply waits for await active.writeQueue. Once every queued chunk has flushed to disk, writer.close() finalizes the stream.
The Results: Flat 20MB RAM at 50GB Scale
Here is what the receiver's memory profile looks like now during a stress test:
| Transfer Size | Old Receiver (In-Memory Blob) | New Receiver (OPFS / FSA Direct) |
|---|---|---|
| 500 MB | ~600 MB Heap | 18 MB Heap |
| 2 GB | ~2.4 GB Heap (Crashed iOS) | 21 MB Heap (Flawless on iOS) |
| 10 GB | Instant Tab Crash | 23 MB Heap |
| 50 GB | Impossible | 24 MB Heap |
Whether you transfer a 10MB PDF or a 50GB 8K ProRes video, the JavaScript heap stays rock-solid under 25MB.
Fuel the Dev (Tip Me a Coffee in USDC) ☕
Building open-source P2P software with zero telemetry, zero corporate sponsors, and zero ads takes a lot of late-night debugging.
If AirStream saved your workflow, eliminated a paid cloud storage subscription, or helped you stream huge files between your devices, consider fueling future development!
You can tap the Tip button directly on air-stream.pages.dev to send 1, 2, or 5 USDC on Base or Polygon (gas is < \$0.01!), or send directly to my EVM address:
0x2cef0473f985156eDe84312Ce0BeB94B602d26E4
Every micro-tip keeps AirStream independent, ad-free, and 100% open-source for everyone.
Let's Discuss!
- Have you hit the per-tab memory ceiling on mobile Safari or Chrome with web apps before?
- What are your favorite patterns for handling massive streams or files in client-side TypeScript?
- What other browser storage quirks have you encountered when working with OPFS or File System Access?
Drop your thoughts below! 👇
⭐ Star the repo on GitHub: github.com/QuantumTechCoder/AirStream
🚀 Stream files live: air-stream.pages.dev
Top comments (2)
Let's Discuss!
Have you hit the per-tab memory ceiling on mobile Safari or Chrome with web apps before?
What are your favorite patterns for handling massive streams or files in client-side TypeScript?
What other browser storage quirks have you encountered when working with OPFS or File System Access?
Drop your thoughts below! 👇
Receiver-side backpressure is the half most people forget, since the sender looks fine and the tab just dies. Did you end up streaming straight to disk through the File System Access API, or chunking in memory with a hard ceiling?