Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Streaming axios response from a get request in nodeJS

I'm searching to send (return) a readableStream to a method (myMethod) with axios in a node program -> i'd like to stream the response to a ReadableStream that I can use to send to the myMethod() callers :

// This code does'nt work but that's what I'm trying to do
function myMethod() {
  var readableStream = createReadableStream(); // does not exists but that's here to show the concept

  const axiosObj = axios({ 
      method: 'get',
      url: 'https://www.google.fr',
      responseType: 'stream' 
  }).then(function(res) {
      res.data.pipe(readableStream);
  }).catch(function(err) {
      console.log(err);
  });

  return readableStream;
}
function myMethodCaller() {
   myMethod().on('data', function(chunk) {
      // Do some stuffs with the chunks
   });
}

I know that we can only do in res.data a pipe to a writableStream. I'm stuck in returning a readableStream usable by the myMethod() callers. Have you got any ideas on that ?

Regards, Blured.

like image 320
Blured Derulb Avatar asked Nov 15 '25 17:11

Blured Derulb


1 Answers

I've found an implementation

var axios = require('axios');
const Transform = require('stream').Transform;

function streamFromAxios() {
    // Create Stream, Writable AND Readable
    const inoutStream = new Transform({
        transform(chunk, encoding, callback) {
            this.push(chunk);
            callback();
        },
    });

    // Return promise
    const axiosObj = axios({
        method: 'get',
        url: 'https://www.google.fr',
        responseType: 'stream'
    }).then(function(res) {
        res.data.pipe(inoutStream);
    }).catch(function(err) {
        console.log(err);
    });

    return inoutStream;
}

function caller() {
    streamFromAxios().on('data', chunk => {
        console.log(chunk);
    });
}

caller();
like image 117
Blured Derulb Avatar answered Nov 18 '25 09:11

Blured Derulb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!