I’m not a big fan of the built-in dgram and net libraries in Nodejs, but it’s really easy to create sockets and write networking applications with Nodejs. One of the biggest issues I’m constantly seeing is the lack of cleanup functionalities when using the socket libraries in Nodejs.

This code is from our DNS npm package that we use in  Violetnorth

You can find the Violetnorth — DNS npm package here:  github.com/violetnorth/dns

So I’m going to talk about a quick way to clean up sockets with timeouts and overall error handling when dealing with sockets.

If you don’t implement some sort of timeout, you are going to run out of available sockets especially when using TCP sockets.

Sockets usually hang and there needs to be timeout handling to close the socket if it’s hanging, which is not super apparent with Nodejs.

#nodejs #sockets #javascript #socketio

Closing TCP/UDP Sockets with Timeouts and Error Handling in Nodejs
7.15 GEEK