All Projects → ceejbot → keep-alive-agent

ceejbot / keep-alive-agent

Licence: MIT license
a keep-alive agent for node http & https with a really snappy name

Programming Languages

javascript
184084 projects - #8 most used programming language

keep-alive-agent

Build Status

keep-alive-agent is an HTTP connection pool agent for node.js that re-uses sockets. It is simpler than some agents that also solve this problem because it does not attempt to replace the Agent provided by node. If you want to re-use connections, use this agent. If you want the default node behavior, use the default global agent.

Update

The node.js bug this module was written to work around was fixed in node 0.8.20. It is still handy as a general keep-alive agent, however.

Usage

new KeepAliveAgent(options-hash)

Create an instance of the agent, passing the options hash through to the node Agent constructor. These options are in turn passed along to createConnection(). The KeepAliveAgent constructor does not use the options itself. The option you are most likely to change is maxSockets, which defaults to 5.

To use the agent instance, set it in the agent field of the options passed to http.request() or http.get(). See the http.request() documentation for details.

Example:

var http = require('http'),
    KeepAliveAgent = require('keep-alive-agent');

var getOptions = {
    hostname: 'twitter.com',
    port: 80,
    path: '/dshaw',
    agent: new KeepAliveAgent(),
};
http.get(getOptions, function(response)
{
	response.pipe(process.stdout);
});

new KeepAliveAgent.Secure(options-hash)

A keep-alive agent that creates tls sockets. Use it the same way you use the http agent.

Example:

var https = require('https'),
    KeepAliveAgent = require('keep-alive-agent');

var getOptions = {
    hostname: 'www.duckduckgo.com',
    port: 443,
    path: '/?q=unicorns',
    agent: new KeepAliveAgent.Secure(),
};
https.get(getOptions, function(response)
{
	response.pipe(process.stdout);
});

See Also

For other implementations, see agentkeepalive and the request module's ForeverAgent.

Licence

MIT.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].