You can use new Agent() method to create an instance of an agent in Node. The http.request() method uses the globalAgent from the 'http' module to create a custom http.Agent instance.
Syntax
new Agent({options})
Parameters
The above function can accept the following Parameters −
options – These options will contain the configurable options that could be set on an Agent while creation. Below are the fields/options the Agent can have −
keepAlive – This method keeps the sockets around whether there are any outstanding requests or not, but keeps them for any future requests without actually re-establishing the TCP connection. One can use 'close' connection' to close this connection. Default value: false.
keepAliveMsecs – On using the keepAlive option as true, this option defines the initial delay for TCP keep-Alive packets. Default value is 1000.
maxSockets – This options defines the maximum number of sockets that are allowed per host. By Default this value is infinity.
maxTotalSockets – Total number of sockets that are allowed for all the hosts. Each requests uses a new socket until the limit is reached. Default value is Infinity.
maxFreeSockets - This is the maximum number of free sockets that can be left open in a free state. Default value is: 256.
scheduling – This is the scheduling strategy that can be applied while picking the next free socket to use. The scheduling can either be 'fifo' or 'lifo'.
timeout – Represents socket timeout in milliseconds.
Example
Create a file with name – agent.js and copy the below code snippet. After creating file, use the following command to run this code as shown in the example below −
node agent.js
agent.js
// Node.js program to demonstrate the creation of new Agent // Importing the http module const http = require('http'); // Creating a new agent var agent = new http.Agent({}); // Defining options for agent const aliveAgent = new http.Agent({ keepAlive: true, maxSockets: 5, }); // Creating connection with alive agent var aliveConnection = aliveAgent.createConnection; // Creating new connection var connection = agent.createConnection; // Printing the connection console.log('Succesfully created connection with agent: ', connection.toString); console.log('Succesfully created connection with alive agent: ', aliveConnection.toString);
Output
C:\home\node>> node agent.js Succesfully created connection with agent: function toString() { [native code] } Succesfully created connection with alive agent: function toString() { [native code] }
Example
The 'agentkeepalive' module provides better flexibility while trying to create a socket or agent. We will use this module in the below example for better understanding.
Installation
npm install agentkeepalive --save
Program Code
// Node.js program to demonstrate the creation of new Agent // Importing the http module const http = require('http'); // Importing the agentkeepalive module const Agent = require('agentkeepalive'); // Creating a new agent const keepAliveAgent = new Agent({}); // Implementing some options const options = { host: 'tutorialspoint.com', port: 80, path: '/', method: 'GET', agent: keepAliveAgent, }; // Requesting details via http server module const req = http.request(options, (res) => { // Printing statuscode, headers and other details // received from the request console.log("StatusCode: ", res.statusCode); console.log("Headers: ", res.headers); }); // Printing the agent options console.log("Agent Options: ", req.agent.options); req.end();
Output
C:\home\node>> node agent.js Agent Options: { socketActiveTTL: 0, timeout: 30000, freeSocketTimeout: 15000, keepAlive: true, path: null } StatusCode: 403 Headers: { date: 'Sun, 25 Apr 2021 08:21:14 GMT', server: 'Apache', 'x-frame-options': 'SAMEORIGIN', 'last-modified': 'Thu, 16 Oct 2014 13:20:58 GMT', etag: '"1321-5058a1e728280"', 'accept-ranges': 'bytes', 'content-length': '4897', 'x-xss-protection': '1; mode=block', vary: 'User-Agent', 'keep-alive': 'timeout=5, max=100', connection: 'Keep-Alive', 'content-type': 'text/html; charset=UTF-8' }