title: How do I send stdout in real time from nodejs to angularjs?
body: <p>I have a script that runs for a long time. It generates an output. I am running this script from nodejs using child_process. How do I send the output of this script soon as it starts executing and do not wait for the script to complete. The code that I currently have waits for the script to complete and then outputs all the stdout at once on nodejs console. </p>
  
  <p>Sample script:</p>
  
  <pre><code>import time
  
  if __name__ == '__main__':
      for i in range(5):
          time.sleep(1)
          print("Hello how are you " + str(i))
  </code></pre>
  
  <p>nodejs code:</p>
  
  <pre><code>var spawn = require('child_process').spawn,
      ls    = spawn('python', ['path/test.py']);
  
  ls.stdout.on('data', function (data) {
    console.log('stdout: ' + data);
  });
  
  ls.stderr.on('data', function (data) {
    console.log('stderr: ' + data);
  });
  
  ls.on('close', function (code) {
    console.log('child process exited with code ' + code);
  });
  </code></pre>
  
  <p>console.log waits for the script to complete and then outputs</p>
  
  <pre><code>Hello how are you 1
  Hello how are you 2
  Hello how are you 3
  Hello how are you 4
  Hello how are you 5
  </code></pre>
  
  <p>in one shot. Is there anyway I can achieve sending stdout immediately as its written until the child process stops? </p>