I would like to create persistent http connection to host (api.development.push.apple.com) and send POST requests for many paths (for example, '/3/device/1', '/3/device/2', etc.). Will code below create one connection with host or many connections for each http.request()?
var http = require('http');
http.request({
host: 'api.development.push.apple.com',
port: 443,
path: '/3/device/1',
method: 'POST',
}).end();
http.request({
host: 'api.development.push.apple.com',
port: 443,
path: '/3/device/2',
method: 'POST'
}).end();
What you want is to use the same Agent for all of your requests.
If you don't specify an agent in the options object, the http module will use the globalAgent, which sets keepAlive to false by default.
So, create your agent, and use it for all requests:
var http = require('http');
var agent = new http.Agent({ keepAlive: true }); // false by default
http.request({
host: 'api.development.push.apple.com',
port: 443,
path: '/3/device/1',
method: 'POST',
agent: agent, // use this agent for more requests as needed
}).end();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With