title: Large file upload with WebSocket
body: <p>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.</p>
  
  <p>Currently, I'm working with something along the lines of:</p>
  
  <pre><code>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);
      }
  }
  </code></pre>
  
  <p>Then, I would upload using:</p>
  
  <pre><code>function Uploader(url, file) {
      var fs = new FileSlicer(file);
      var socket = new WebSocket(url);
  
      socket.onopen = function() {
          for(var i = 0; i &lt; fs.slices; ++i) {
              socket.send(fs.getNextSlice()); // see below
          }
      }
  }
  </code></pre>
  
  <p>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.</p>