title: nodejs/express - stream stdout instantly to the client
body: <p>I spawned the following child: <code>var spw = spawn('ping', ['-n','10', '127.0.0.1'])</code> and I would like to receive the ping results on the client side (<strong>browser</strong>) <strong>one by one</strong>, not as a whole.</p>
  
  <p>So far I tried this:</p>
  
  <pre><code>app.get('/path', function(req, res) {
     ...
     spw.stdout.on('data', function (data) {
        var str = data.toString();
        res.write(str + "\n");
     });
     ...
  }
  </code></pre>
  
  <p>and that:</p>
  
  <pre><code>...
  spw.stdout.pipe(res);
  ...
  </code></pre>
  
  <p>In both cases browser waits 10 of the pings to complete, and then prints the result as a whole. I would like to have them one by one, how to accomplish that?</p>
  
  <p>(Client is just making a call to <code>.../path</code> and console.logs the result)</p>
  
  <hr>
  
  <p><strong>EDIT:</strong> Although I do believe that websockets are necessary to implement this, I just want to know whether there are any other ways. I saw several confusing <a href="https://stackoverflow.com/a/20357555/1089987">SO answers</a>, and blog posts (in <a href="http://new-bamboo.co.uk/blog/2009/12/08/real-time-online-activity-monitor-example-with-node-js-and-websocket" rel="noreferrer">this</a> post, at step one OP streams the logs to the browser) which didn't help, therefore I decided to go for a bounty for some attention.</p>