Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 737
Ofer B Avatar asked Jan 01 '23 00:01

Ofer B


2 Answers

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

}
like image 71
Ilan Frumer Avatar answered Jan 03 '23 00:01

Ilan Frumer


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

import nodeFetch, { RequestInfo, RequestInit } 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 = {}) => {
  return nodeFetch(url, {
    agent: (parsedURL) => {
      if (parsedURL.protocol === "http:") {
        return httpAgent;
      } else {
        return httpsAgent;
      }
    },
    ...options
  });
};
like image 31
Etienne Martin Avatar answered Jan 02 '23 23:01

Etienne Martin