Open In App
Related Articles

Node.js agent.maxSockets Method

Improve Article
Save Article
Like Article

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.maxSockets (Added in v0.3.6) method is an inbuilt application programming interface of the ‘Http‘ module which determines how many concurrent sockets the agent can have open per origin. Origin is the returned value of agent.getName().

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


const http = require('http');



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

Return Value <number>: By default, it is set Infinity. It determines how many concurrent sockets the agent can have open per origin.

The below example illustrates the use of agent.maxSockets method in Node.js.

Example 1: Filename: index.js

// Node.js program to demonstrate the 
// agent.maxSockets method 
// Importing http module 
const http = require('http');
// Importing agentkeepalive module 
const Agent = require('agentkeepalive'); 
// Creating new agent 
const keepAliveAgent = new Agent({}); 
// Options object 
const options = { 
  host: ''
  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); 

Run index.js file using the following command:

node index.js



StatusCode:  301


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 01 Oct, 2020
Like Article
Save Article
Similar Reads
Complete Tutorials