Streaming data events aren't registered

2019-07-11 13:05发布

I'm using superagent to receive a notifications stream from a server

require('superagent')
  .post('www.streaming.example.com')
  .type('application/json')
  .send({ foo: 'bar' })
  .on('data', function(chunk) {
    console.log('chunk:' + chunk); // nothing shows up
  })
  .on('readable', function() {
    console.log('new data in!');   // nothing shows up
  })
  .pipe(process.stdout);           // data is on the screen

For some reason data and readable events aren't registered, hovewer I can pipe data to the sceeen. How can I process data on the fly?

2条回答
Bombasti
2楼-- · 2019-07-11 13:44

Looking at the source of pipe method, you can get access to the original req object and add listeners on it:

require('superagent')
  .post('www.streaming.example.com')
  .type('application/json')
  .send({ foo: 'bar' })
  .end().req.on('response',function(res){
      res.on('data',function(chunk){
          console.log(chunk)
      })
      res.pipe(process.stdout)
  })

But this won't handle the redirection if any.

查看更多
看我几分像从前
3楼-- · 2019-07-11 13:45

It looks like superagent doesn't return a real stream, but you can use something like through to process the data:

var through = require('through');

require('superagent')
  .post('www.streaming.example.com')
  .type('application/json')
  .send({ foo: 'bar' })
  .pipe(through(function onData(chunk) {
    console.log('chunk:' + chunk); 
  }, function onEnd() {
    console.log('response ended');
  }));

(although you have to check if superagent won't first download the entire response before it sends data through the pipe)

查看更多
登录 后发表回答