title: Using streams in NodeJS
body: <p>I'm trying to build an API in Nodejs that will stream the output of a query that is executed against a vertica DB.</p>
  <p>The vertica db driver in Nodejs exposes unbuffered query interface that I'm using. For more details about that please see:
  <a href="https://github.com/wvanbergen/node-vertica" rel="nofollow noreferrer">https://github.com/wvanbergen/node-vertica</a></p>
  <p>Following is my code:</p>
  <pre><code>var vertica = require('vertica');
  var Readable = require('stream').Readable;
  var rs = new Readable;
  
  var conn = vertica.connect( {
      host: 'hostname',
      user: 'user',
      password: 'password',
      database: 'verticadb'    
  });
  
  
  var q = conn.query('select * from table');
  
  q.on('row', function(row) {
      rs.push(row.join(',') + &quot;\n&quot;);
  });
  
  q.on('end', function(status) {
      rs.push(null);
      rs.pipe(process.stdout);
      conn.disconnect();
  });
  
  q.on('error', function(err) {
      conn.disconnect();
  });
  </code></pre>
  <p>It does return the appropriate output, but my understanding is that it actually buffers up the output of <code>row.join(',') + &quot;\n&quot;</code> and only pipes it out to stdout once all rows are read. My objective is to pipe each row out as soon as it is read. How should I modify my code to make it work ? You can replace the vertica &quot;row&quot; event with anything comparable.</p>
  <h2>Addendum</h2>
  <p>I've managed to make it work using what is called &quot;classic readable streams&quot; based on the documentation found on: <a href="https://github.com/substack/stream-handbook" rel="nofollow noreferrer">https://github.com/substack/stream-handbook</a>.</p>
  <p>The code for this:</p>
  <pre><code>var vertica = require('vertica');
  var Stream = require('stream');
  var stream = new Stream;
  stream.readable = true;
  
  var conn = vertica.connect( {
      host: 'hostname',
      user: 'user',
      password: 'password',
      database: 'verticadb'
  });
  
  
  var q = conn.query('select * from affiliate_manager_2');
  
  q.on('row', function(row) {
      stream.emit('data', row.join(',') + &quot;\n&quot;);
  });
  
  q.on('end', function(status) {
      stream.emit('end'); 
      conn.disconnect();
  });
  
  q.on('error', function(err) {
      conn.disconnect();
  });
  
  stream.pipe(process.stdout);
  </code></pre>
  <p>However it is the &quot;old&quot; way of doing it, and I would like to know how to do it using the &quot;new way&quot;.</p>