title: How do I make data from node's process.stdout.on stream globally accessible?
body: <p>This is a js function in my server code that has a child process which streams data from a python script:</p>
  
  <pre><code>    var dataString = '';
  sampleFunc = function(){
      var spawn = require('child_process').spawn,
          py = spawn('python', ['F:/sample.py']),
          data = [1,2,3,4,5,6,7,8,9];
  
      py.stdout.on('data', function(data){
              console.log("before data write");
                dataString += data.toString();
              });
  
          py.stdout.on('end', function(){
              console.log("successful");
              dataStr = dataString;
                console.log(dataString);
              });
  
          py.stdin.write(JSON.stringify(data));
          console.log("inside stdin");    
          py.stdin.end();
  
  };
  </code></pre>
  
  <p>Then i'm using socket.io to send this data to a client. But dataStr is always empty outside of stdout. </p>
  
  <pre><code>io.on('connection', function(socket){
  sampleFunc();
  console.log(dataStr);
  console.log('a user connected');
  
  });
  </code></pre>
  
  <p>I simply need to get the data coming into the stream at:</p>
  
  <pre><code>py.stdout.on('data', function(data){
                  console.log("before data write");
                    dataString += data.toString();
                  });
  </code></pre>
  
  <p>and be able to access it inside the socket code. Any help in solving this would be much appreciated...</p>