Search code examples
axioskeep-alive

Connecting to multiple servers using axios keep alive


I'm refactoring a server that pulls information from several different APIs to use a keep-alive connection. The only example I've seen that uses keep alive with axios uses a single axios instance with an agent that has keep alive set to true. If I'm connecting to multiple api's, that are all run on different servers, can I use a single axios instance or would I have to create an instance for each server that I connect to?


Solution

  • After considerable testing (we set up a server to connect to several host simultaneously and let it run for a bit) it looks like you only need a single axios instance and a single agent to handle any number of hosts. As a side note, the agentkeepalive package is a good agent to use because it handles cleaning up sockets that the https agent package doesn't