Friday, October 24, 2025
HomeLanguagesJavascriptNode.js agent.maxFreeSockets Method

Node.js agent.maxFreeSockets Method

The Node.js HTTP API is low-level so that it could support the HTTP applications. In order to access and use the HTTP server and client, we need to call them (by ‘require(‘http’)‘). HTTP message headers are represented as JSON Format.

The agent.maxFreeSockets (Added in v0.11.7) method is an inbuilt application programming interface of the ‘Http‘ module which sets the maximum number of sockets that will be left open in the free state. 

In order to get a response and a proper result, we need to import ‘http’ module.

Import:

const http = require('http');

Syntax:

agent.maxFreeSockets;

Parameters: This function does not accept any parameters as mentioned above.

Return Value <number>: By default, it is set to 256. For agents with keepAlive enabled, this sets the maximum number of sockets that will be left open in the free state.

The below examples illustrate the use of agent.maxFreeSockets method in Node.js.

Example 1: Filename: index.js




// Node.js program to demonstrate the 
// agent.maxFreeSockets method 
  
// Importing http module 
const http = require('http'); 
  
// Importing agentkeepalive module 
const Agent = require('agentkeepalive'); 
  
// Creating new agent 
const keepAliveAgent = new Agent({}); 
  
console.log(keepAliveAgent.maxFreeSockets);
  
// Options object 
const options = { 
  host: 'geeksforgeeks.org'
  port: 80, 
  path: '/'
  method: 'GET'
  agent: keepAliveAgent, 
}; 
  
// Requesting via http server module 
const req = http.request(options, (res) => { 
  // Printing statuscode 
  console.log("StatusCode: ", res.statusCode); 
}); 
  
req.end(); 


Run index.js file using the following command:

node index.js

Output:

256

StatusCode:  301

Reference: https://nodejs.org/api/http.html#http_agent_maxfreesockets

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS