title: How to stream data over socket.io to client
body: <p>I have socket.io sending a basic object from server to client. This bit works fine.</p>
  
  <p>Now want to send a stream from server to client, using <a href="https://github.com/dominictarr/event-stream" rel="nofollow noreferrer">event-stream</a> (specifically the results of a block-chain query). I am getting unexpected results in the browser console..</p>
  
  <pre><code>    var io = require('socket.io')(server);
      var dsteem = require('dsteem')
      var es = require('event-stream') 
      var util = require('util')
      var client = new dsteem.Client('https://api.steemit.com')
      var stream = client.blockchain.getBlockStream()
  
  /* This sends results to stdout, fine
  io.on('connection', function(socket){
      stream.pipe(es.map(function(block, callback) {
          callback(null, util.inspect(block) + '\n') 
      })).pipe(process.stdout);
  
      // And this sends a simple object to the client 
      socket.emit('blockchainOps', {"Foo!":"Doo!"} );
  });
  */
  
  // Putting both together sends strange connection data to client
  io.on('connection', function(socket){
  
          socket.emit('blockchainOps', function() {
  
              stream.pipe(es.map(function(block, callback) {
                  callback(null, util.inspect(block) + '\n');
              }))
          })
  
      });
  </code></pre>
  
  <p>What I get in the client console appears to be some kind of TCP socket function,</p>
  
  <p><code>ƒ (){if(!n){n=!0;var r=a(arguments);u("sending ack %j",r),e.packet({type:i.ACK,id:t,data:r})}}</code></p>
  
  <p>Can anyone help me understand what's going on and what I'm doing wrong?</p>
  
  <p>== EDIT UPDATE ==</p>
  
  <p>As suggested in comments, I've tried <a href="https://github.com/nkzawa/socket.io-stream" rel="nofollow noreferrer">socket.io-stream</a> to augment <a href="https://github.com/dominictarr/event-stream" rel="nofollow noreferrer">event-stream</a>.</p>
  
  <pre><code>var es = require('event-stream') 
  var util = require('util')
  var ss = require('socket.io-stream'); 
  var stream = ss.createStream();
  
  io.on('connection', function(socket){
  
  ss(socket).emit('blockchainOps', stream, function(){
  
  
          client.blockchain.getBlockStream()
              .pipe(es.map(function(block, callback) {
                  callback(null, util.inspect(block) + '\n') 
                  }))
              .pipe(process.stdout)
  
  
  }());
  
  });
  </code></pre>
  
  <p>This time I get a socket object returned in the browser console which does not seem to be the stream data I was hoping for.</p>
  
  <p><a href="https://i.stack.imgur.com/WNZgb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WNZgb.png" alt="enter image description here"></a></p>