Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Large file upload with WebSocket

I'm trying to upload large files (at least 500MB, preferably up to a few GB) using the WebSocket API. The problem is that I can't figure out how to write "send this slice of the file, release the resources used then repeat". I was hoping I could avoid using something like Flash/Silverlight for this.

Currently, I'm working with something along the lines of:

function FileSlicer(file) {     // randomly picked 1MB slices,     // I don't think this size is important for this experiment     this.sliceSize = 1024*1024;       this.slices = Math.ceil(file.size / this.sliceSize);      this.currentSlice = 0;      this.getNextSlice = function() {         var start = this.currentSlice * this.sliceSize;         var end = Math.min((this.currentSlice+1) * this.sliceSize, file.size);         ++this.currentSlice;          return file.slice(start, end);     } } 

Then, I would upload using:

function Uploader(url, file) {     var fs = new FileSlicer(file);     var socket = new WebSocket(url);      socket.onopen = function() {         for(var i = 0; i < fs.slices; ++i) {             socket.send(fs.getNextSlice()); // see below         }     } } 

Basically this returns immediately, bufferedAmount is unchanged (0) and it keeps iterating and adding all the slices to the queue before attempting to send it; there's no socket.afterSend to allow me to queue it properly, which is where I'm stuck.

like image 937
Vlad Ciobanu Avatar asked Jun 18 '12 09:06

Vlad Ciobanu


People also ask

Can we send file through WebSocket?

First, APIs are needed to load the file from the file system, then the file is transformed into a suitable payload to be sent over web sockets, and finally, server-side code is required to receive the file.

Does WebSocket consume bandwidth?

It can be shown in bandwidth usage and memory usage. In the experiment was found that the average of bandwidth usage is 478KB for polling method, and 91KB for web socket method in web based and the memory consumption of websocket less as much as 16% compared to polling method.

Are WebSockets faster than API?

Conclusion. The benchmarks I ran are by no means a comprehensive analysis of all the nuances in the performance difference between HTTP and websockets. It did however confirm my initial impression that for many cases websockets can be faster than a traditional HTTP API.

How much traffic can a WebSocket handle?

The theoretical limit is 65k connections per IP address but the actual limit is often more like 20k, so we use multiple addresses to connect 20k to each (50 * 20k = 1 mil).


2 Answers

I believe the send() method is asynchronous which is why it will return immediately. To make it queue, you'd need the server to send a message back to the client after each slice is uploaded; the client can then decide whether it needs to send the next slice or a "upload complete" message back to the server.

This sort of thing would probably be easier using XMLHttpRequest(2); it has callback support built-in and is also more widely supported than the WebSocket API.

like image 72
Graham Avatar answered Sep 30 '22 19:09

Graham


Use web workers for large files processing instead doing it in main thread and upload chunks of file data using file.slice().

This article helps you to handle large files in workers. change XHR send to Websocket in main thread.

//Messages from worker function onmessage(blobOrFile) {  ws.send(blobOrFile); }  //construct file on server side based on blob or chunk information. 
like image 23
kongaraju Avatar answered Sep 30 '22 18:09

kongaraju