PageRenderTime 55ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/README.md

https://github.com/borispovod/keep-alive-agent
Markdown | 65 lines | 45 code | 20 blank | 0 comment | 0 complexity | 099eeeb9bd087051f17b0be78bea79b1 MD5 | raw file
  1. # keep-alive-agent
  2. [![Build Status](https://travis-ci.org/ceejbot/keep-alive-agent.png)](https://travis-ci.org/ceejbot/keep-alive-agent)
  3. keep-alive-agent is an HTTP connection pool [agent](http://nodejs.org/api/http.html#http_class_http_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.
  4. ## Update
  5. The [node.js bug](https://github.com/joyent/node/issues/4373) 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.
  6. ## Usage
  7. __new KeepAliveAgent(<i>options-hash</i>)__
  8. 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.
  9. 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](http://nodejs.org/api/http.html#http_http_request_options_callback) for details.
  10. Example:
  11. ```javascript
  12. var http = require('http'),
  13. KeepAliveAgent = require('keep-alive-agent');
  14. var getOptions = {
  15. hostname: 'twitter.com',
  16. port: 80,
  17. path: '/dshaw',
  18. agent: new KeepAliveAgent(),
  19. };
  20. http.get(getOptions, function(response)
  21. {
  22. response.pipe(process.stdout);
  23. });
  24. ```
  25. __new KeepAliveAgent.Secure(<i>options-hash</i>)__
  26. A keep-alive agent that creates tls sockets. Use it the same way you use the http agent.
  27. Example:
  28. ```javascript
  29. var https = require('https'),
  30. KeepAliveAgent = require('keep-alive-agent');
  31. var getOptions = {
  32. hostname: 'www.duckduckgo.com',
  33. port: 443,
  34. path: '/?q=unicorns',
  35. agent: new KeepAliveAgent.Secure(),
  36. };
  37. https.get(getOptions, function(response)
  38. {
  39. response.pipe(process.stdout);
  40. });
  41. ```
  42. ## See Also
  43. For other implementations, see [agentkeepalive](https://github.com/TBEDP/agentkeepalive) and the [request](https://github.com/mikeal/request) module's [ForeverAgent](https://github.com/mikeal/request/blob/master/forever.js).
  44. ## Licence
  45. MIT.