6

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.

Ofer B
  • 369
  • 2
  • 5
  • 14

4 Answers4

20

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];

}
Ilan Frumer
  • 32,059
  • 8
  • 70
  • 84
2

Here's a wrapper around node-fetch based on their documentation:

import nodeFetch, { RequestInfo, RequestInit, Response } from "node-fetch";
import http from "http";
import https from "https";

const httpAgent = new http.Agent({
  keepAlive: true
});

const httpsAgent = new https.Agent({
  keepAlive: true
});

export const fetch = (url: RequestInfo, options: RequestInit = {}): Promise<Response> => {
  return nodeFetch(url, {
    agent: (parsedURL) => {
      if (parsedURL.protocol === "http:") {
        return httpAgent;
      } else {
        return httpsAgent;
      }
    },
    ...options
  });
};
MDMower
  • 808
  • 8
  • 27
Etienne Martin
  • 10,018
  • 3
  • 35
  • 47
0

Keep-alive is not enabled for the default used agent and is not currently implemented into node-fetch directly, but you can easily specify a custom-agent where you enable the keep-alive option:

const keepAliveAgent = new http.Agent({
    keepAlive: true
});

fetch('{API endpoint}', {
     ...
     agent: keepAliveAgent         
});
eol
  • 23,236
  • 5
  • 46
  • 64
0

here is a wrapper for node-fetch to add a keepAlive option, based on Ilan Frumer's answer

// fetch: add option keepAlive with default true
const fetch = (function getFetchWithKeepAlive() {
  const node_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 });
  return async function (url, userOptions) {
    const options = { keepAlive: true };
    Object.assign(options, userOptions);
    if (options.keepAlive == true)
      options.agent = (parsedUrl => parsedUrl.protocol == 'http:' ? httpAgent : httpsAgent);
    delete options.keepAlive;
    return await node_fetch(url, options);
  }
})();

const response = await fetch('https://github.com/');
const response = await fetch('https://github.com/', { keepAlive: false });
milahu
  • 2,447
  • 1
  • 18
  • 25