Search code examples
node.jshttpnode-fetch

Reuse TCP connection with node-fetch in node.js


I am using this function to call an external API

const fetch = require('node-fetch');

fetchdata= async function (result = {}) {
  var start_time = new Date().getTime();

    let response = await fetch('{API endpoint}', {
      method: 'post',
      body: JSON.stringify(result),
      headers: { 'Content-Type': 'application/json' },
      keepalive: true

    });

  console.log(response) 
  var time = { 'Respone time': + (new Date().getTime() - start_time) + 'ms' };
  console.log(time)
  return [response.json(), time];
  
}

The problem is that i am not sure that node.js is reusing the TCP connection to the API every time i use this function, eventhough i defined the keepalive property.

Reusing the TCP connection can significantly improve response time
Any suggestions will be welcomed.


Solution

  • As documented in https://github.com/node-fetch/node-fetch#custom-agent

    const fetch = require('node-fetch');
    
    const http = require('http');
    const https = require('https');
    
    const httpAgent = new http.Agent({ keepAlive: true });
    const httpsAgent = new https.Agent({ keepAlive: true });
    const agent = (_parsedURL) => _parsedURL.protocol == 'http:' ? httpAgent : httpsAgent;
    
    const fetchdata = async function (result = {}) {
        var start_time = new Date().getTime();
    
        let response = await fetch('{API endpoint}', {
            method: 'post',
            body: JSON.stringify(result),
            headers: { 'Content-Type': 'application/json' },
            agent
        });
    
        console.log(response)
        var time = { 'Respone time': + (new Date().getTime() - start_time) + 'ms' };
        console.log(time)
        return [response.json(), time];
    
    }