title: Chunking WebSocket Transmission
body: <p>since I'm using WebSocket connections on more regular bases, I was interested in how things work under the hood. So I digged into the endless spec documents for a while, but so far I couldn't really find anything about <em>chunking the transmission stream itself</em>.</p>
  
  <p>The WebSocket protocol calls it <em>data frames</em> (which describes the pure data stream, so its also called <em>non-control frames</em>). As far as I understood the spec, there is no defined max-length and no defined MTU (maximum transfer unit) value, that in turn means a single WebSocket data-frame may contain, by spec(!), an infinite amount of data (please correct me if I'm wrong here, I'm still a student on this).</p>
  
  <p>After reading that, I instantly setup my little <em>Node</em> WebSocket server. Since I have a strong <em>Ajax</em> history (also on streaming and Comet), my expectations originaly were like, "<em>there must be some kind of interactive mode for reading data while it is transfered</em>". But I am wrong there, ain't I ?</p>
  
  <p>I started out small, with <em>4kb</em> of data. </p>
  
  <p><strong>server</strong></p>
  
  <pre><code>testSocket.emit( 'data', new Array( 4096 ).join( 'X' ) );
  </code></pre>
  
  <p>and like expected this arrives on the client as one data-chunk</p>
  
  <p><strong>client</strong></p>
  
  <pre><code>wsInstance.onmessage = function( data ) {
      console.log( data.length ); // 4095
  };
  </code></pre>
  
  <p>so I increased the payload and I actually was expecting again, that at some point, the client-side <code>onmessage</code> handler will fire repeatly, effectivley chunking the transmission. But to my shock, it never happened (<em>node-server</em>, tested on <em>firefox</em>, <em>chrome</em> and <em>safari</em> client-side). My biggest payload was <strong>80 MB</strong></p>
  
  <pre><code>testSocket.emit( 'data', new Array( 1024*1024*80 ).join( 'X' ) );
  </code></pre>
  
  <p>and it still arrived in one big data-chunk on the client. Of course, this takes a while even if you have a pretty good connection. Questions here are</p>
  
  <ul>
  <li><em>is there any possiblity to chunk those streams, similar to the XHR readyState3 mode</em> ?  </li>
  <li><em>is there any size limit for a single ws data-frame</em> ?  </li>
  <li><em>are websockets not supposed to transfer such large payloads? (which would make me wonder again why there isn't a defined max-size)</em></li>
  </ul>
  
  <p>I might still look from the wrong perspective on WebSockets, probably the need for sending large data-amounts is just not there and you should chunk/split any data logically yourself before sending ?</p>