This is the default TCP connection termination sequence,
By calling client.end()
the node js will send the FIN packet to the server, and the server will response with a FIN packet to the client to accept the socket termination.
As of nodejs documentation what socket.end
does is,
Half-closes the socket. i.e., it sends a FIN packet. It is possible the server will still send some data.
When the FIN packet is received the connection to the server from client is closed automatically and the socket.on('close', .. )
is fired and the ACK is sent back.
So the connection is terminated by agreeing both server and client so the server is able to send data that may require before closing the connection.
But when calling socket.destroy
, the client connection will be destroyed without receiving the FIN packet forcefully. It is a best practice to avoid calling socket.destroy
if possible.
Reference:
client.close()
? – Valvulitis