title: Run script from node.js, capture stdout, send via web socket
body: <p>I am calling a Ruby script through node.js which returns data via stdout, I have managed to get node to send <em>some</em> of the data through to its web socket but I am missing lines and don't really understand much about what I'm doing. From research it looks like I'm working with a stream but I can't get my head round much of the documentation as this is all very new and over my head :(</p>
  
  <p>My Ruby script outputs as follows to stdout when run via a bash shell;</p>
  
  <pre class="lang-none prettyprint-override"><code>{"element":"SENS02","data":{"type":"SEN","descr":"T100"}}
  {"element":"SENS01","data":{"type":"SEN","descr":"    "}}
  {"element":"LED1","data":{"type":"LED","state":"1"}}
  {"element":"LED2","data":{"type":"LED","state":"0"}}
  </code></pre>
  
  <p>When run at the console everything is good - when I call it via my node.js script I am only getting every 2nd line so for example node would output;</p>
  
  <pre class="lang-none prettyprint-override"><code>{"element":"SENS02","data":{"type":"SEN","descr":"T100"}}
  {"element":"LED1","data":{"type":"LED","state":"1"}}
  </code></pre>
  
  <p>My node.js script section is below;</p>
  
  <pre class="lang-js prettyprint-override"><code>var cp = require('child_process');
  var tail = cp.spawn('test.rb');
  
  // Get updates from ruby script //
  tail.stdout.on('data', function(chunk) {
     var pumper = chunk.toString().slice(0,58);
     var closer = JSON.parse(pumper);
     socket.emit('MAP.room1', closer);
  });
  </code></pre>
  
  <p>I also get a crash once in a while with the following error as well if anyone can help;</p>
  
  <blockquote>
    <p>undefined:0</p>
    
    <p>^ SyntaxError: Unexpected end of input
        at Object.parse (native)</p>
  </blockquote>
  
  <p>I need to be able to output every line as received from my ruby script. Anyone who answers please use my own code as above not random examples as it only confuses me hugely when trying to understand what relates to what and 'what it means for me'. I've tried looking at .pipe but all the examples confuse me and I don't understand how to adapt them for my own scripts. What is output from ruby is JS Objects 1 line at a time, I need my web socket to emit exactly the same thing 1 object at a time.</p>
  
  <p>Having done <code>console.log(chunk.toString())</code> to get a readable output
  it has relieved something odd - an extra line feed or something.</p>
  
  <p>Output from console is;</p>
  
  <pre><code>{"element":"SENS02","data":{"type":"SEN","descr":"T100"}}
  {"element":"SENS01","data":{"type":"SEN","descr":"    "}}
  
  {"element":"LED1","data":{"type":"LED","state":"1"}}
  {"element":"LED2","data":{"type":"LED","state":"0"}}
  </code></pre>
  
  <p>How can I get round this? And could this be causing my other 'Unexpected end of input' error?</p>
  
  <p>If I do <code>var pumper = chunk.slice(0,-1); console.log(pumper.toString())</code> then I get the output as shown above with out the odd/extra/rouge line breaks. I'm still no further forward to getting this to output exactly that to the web socket though.</p>
  
  <p>I can get this out of the web socket but it shows I have \n still somehow?!?! </p>
  
  <p><code>["{\"element\":\"SENS02\",\"data\":{\"type\":\"SEN\",\"descr\":\"T100\"}}\n{\"element\":\"SENS01\",\"data\":{\"type\":\"SEN\",\"descr\":\"    \"}}"]}</code>
  I need to get that back into single flat line like I have at my console...</p>
  
  <p>So having done as suggested below I now get the desired output by using this code. I still get <code>): Unexpected end of input</code> logged in the console, that appears to be from additional line feeds I get from my Ruby script. Having run the script for a couple of days without crashes it <em>appears</em> all the data is getting through intact and parsed out to the web socket. I still need to read up and fully understand what the <code>for</code> is doing, but otherwise it appears to have fixed my issue.</p>
  
  <pre><code>tail.stdout.on('data', function(chunk) {
  var closer = chunk.toString()
  
  var sampArray = closer.split('\n');
  
   for (var i = 0; i &lt; sampArray.length; i++) {
    try {
     var newObj = JSON.parse(sampArray[i]);
  socket.emit('MAP.room1', newObj);
     } catch (err) {
  console.log('): ' + err.message);
    }
   }
  });
  </code></pre>