node-cassandra-cql icon indicating copy to clipboard operation
node-cassandra-cql copied to clipboard

How can I handle streams backpressure?

Open idandagan1 opened this issue 5 years ago • 1 comments

Hi, Let's say I have an express app and I'd like to use streams.

For example:

const express = require('express');
const app = express();

app.use((req, res, next) => {
  client.stream('SELECT time, val FROM temperature WHERE station_id=', [ 'abc' ])
    .on('readable', () => {
      let row;
      while (row = this.read()) {
         res.write(row); // THIS SHOULD BE PIPED
      }
    })
    .on('end', function () {})
    .on('error', function (err) {});
})

So in Node.js v10.0.0, they added support for pipeline which uses the benefits of pipe for automatic pause/resume logic - but also forwarding errors and properly cleaning up streams if needed.

How can I handle backpressure?

idandagan1 avatar Sep 22 '20 06:09 idandagan1

Express itself hast a streaming api as well. I guess if you use that you are already covered?

app.use((req, res, next) => {
  client.stream('SELECT time, val FROM temperature WHERE station_id=', [ 'abc' ]).pipe(res);
});

haven't tried it, but i guess it should work :)

bitcloud avatar Sep 23 '20 19:09 bitcloud