You've hit on two very important and insightful questions! Let's address them separately:
1. TCP Handshakes and Web Workers
No, you absolutely do not need to keep alive all 500 TCP handshakes simultaneously. That would be a disaster! Here's why:
- HTTP/1.1 Keep-Alive (and HTTP/2 Multiplexing): Modern browsers and servers typically use HTTP Keep-Alive (HTTP/1.1) or HTTP/2.
- Keep-Alive: Allows multiple requests to be sent over the same TCP connection. The connection isn't torn down after each request. The browser and server negotiate a keep-alive timeout.
- HTTP/2: Goes even further with multiplexing. It allows multiple requests and responses to be sent concurrently over a single TCP connection.
- Browser Management: The browser (and the
fetch API) handles the underlying TCP connections and HTTP protocol details. You don't need to explicitly manage them in your JavaScript code.
- Worker's Role: The Web Worker initiates a
fetch request for each batch of files. The browser handles establishing (or reusing) the TCP connection and sending the HTTP request. Once the response is received, the browser can either keep the connection alive (if Keep-Alive is enabled) or close it.
Therefore, you'll have a relatively small number of TCP connections open at any given time – likely a handful – even though you're uploading 500 files. The browser intelligently manages the connections for you. You don't need to worry about keeping 500 connections open simultaneously.
2. Uploading File Paths vs. File Contents (Memory Management)
Yes, you can absolutely use only the file paths from the file dialog and upload the files in memory only as needed! This is a critical optimization for memory management. Here's how it works:
- File API Access: The
File object returned by the file input element doesn't contain the entire file contents in memory. It contains metadata (name, size, type) and a way to access the file data on demand.
- FileReader API: You use the
FileReader API to read the file contents into memory only when you need to upload them.
Here's how you'd modify the worker code:
self.addEventListener('message', (event) => {
const fileList = event.data;
const batchSize = 5;
let currentIndex = 0;
function uploadBatch() {
const batch = Array.from(fileList).slice(currentIndex, currentIndex + batchSize);
currentIndex += batchSize;
Promise.all(batch.map(file => {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = (e) => {
// e.target.result contains the file contents as an ArrayBuffer
const formData = new FormData();
formData.append('file', new Blob([e.target.result])); // Create a Blob from the ArrayBuffer
fetch('/api/upload', {
method: 'POST',
body: formData,
})
.then(response => {
if (!response.ok) {
reject(new Error('File upload failed'));
} else {
resolve();
}
})
.catch(error => {
reject(error);
});
};
reader.onerror = (error) => {
reject(error);
};
reader.readAsArrayBuffer(file); // Read the file contents asynchronously
});
}))
.then(responses => {
// Handle responses (success/error)
// ...
})
.catch(error => {
// Handle error
// ...
});
}
uploadBatch();
});