title: Send buffer for audiocontext via socket.io (node.js)
body: <p>I am trying to send audio file (or chunk) via socket.io.</p>
  
  <p>I tried:</p>
  
  <pre><code>// server
      socket.on('client-get-audio', function (data) {
        //data.fname - name of requested file
        var buffer = fs.readFileSync(data.fname, null);
        // try 2 - read arraybuffer
        //var buffer = fs.readFileSync(data.fname, null).buffer;
        buffer = new Uint8Array(buffer);
        socket.emit('audio-data', {count: 1, buff: buffer});
      });
  </code></pre>
  
  <p>I can not decode data:</p>
  
  <pre><code>// client
  // ctx - AudioContext
  // source - buffer source
  socket.on('audio-data', function(data){
    ctx.decodeAudioData(data.buff, decoded =&gt; {
    source.buffer = decoded;
    source.start(0);
  });
  </code></pre>
  
  <p>I will not describe all my attempts, there were a lot of them.</p>
  
  <p><strong>update</strong></p>
  
  <p>working variant</p>
  
  <pre><code>// server
  socket.on('client-get-audio', function (data) {
      //data.fname - name of requested file
      var buffer = fs.readFileSync(data.fname, null);
      socket.emit('audio-data', {count: 1, buff: buffer});
  });
  
  
  // client
  
    function toArrayBuffer(buf) {
        var ab = new ArrayBuffer(buf.length);
        var view = new Uint8Array(ab);
        for (var i = 0; i &lt; buf.length; ++i) {
            view[i] = buf[i];
        }
        return ab;
    }
  
  // ctx - AudioContext
  // source - buffer source
  socket.on('audio-data', function(data){
    ctx.decodeAudioData(toArrayBuffer(data.buff.data), decoded =&gt; {
    source.buffer = decoded;
    source.start(0);
  });
  </code></pre>
  
  <p>Maybe someone can suggest a more correct variant?</p>