0

I’m building a Node.js HTTP client that needs to:

  • Use mutual TLS (mTLS)
  • Enable keep-alive, so sockets are reused across requests.
  • Set a keep-alive idle timeout to automatically close unused sockets after a period of inactivity

I'd like to configure this once using a single shared https.Agent instance and pass it to the global HTTP module (e.g., via Axios or NestJS HttpModule) so that all outbound requests automatically use this agent

While reviewing the Node.js documentation, I came across this line for the timeout option:

"Socket timeout in milliseconds. This will set the timeout when the socket is created." This is not clear to me.

I’m confused on a few things and would appreciate any guidance or clarification on this setup :

  • What does it mean by Socket timeout and why does it say "when the socket is created" specifically?
  • Also Is this the best approach if I want to set it once and share across requests/modules?

Code Snippet for reference:

import { Agent } from 'https';
import * as fs from 'fs';

const httpsAgent = new Agent({
  keepAlive: true,
  key: fs.readFileSync('client.key'),
  cert: fs.readFileSync('client.crt'),
  passphrase: 'passphrase',
  timeout: 5000
});

HttpModule.register({
  httpsAgent,
});

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.