Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set a socket connection timeout in node.js http client

Is there a way to set a socket connection timeout in node.js http.request?

I'm not looking to set a http request timeout but a connection timeout.

like image 906
bearnard Avatar asked Feb 22 '26 16:02

bearnard


1 Answers

Use req.socket.setTimeout(30000); This overrides the default of having no timeouts for sockets. Be aware that the timeout event will not automatically close the connection, you'll have to do that yourself when you handle the event, usually by calling end() or destroy on the socket. You can also add a one-time-callback as an optional parameter.

like image 75
jupp0r Avatar answered Feb 24 '26 11:02

jupp0r



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!