when does 'end' or 'close' get called on tls.connect

254
June 15, 2017, at 10:47 AM

For examples like this: https://bravenewmethod.com/2011/02/21/node-js-tls-client-example/

or in my own code:

client = tls.connect(port, host, tlsOptions, function() {
}
client.on('end', function(data) {
}

When do these lifecycle methods get actually called? In the documentation, https://nodejs.org/api/tls.html, I don't see anything about it. Thanks.

Answer 1

You need to be looking in the doc for the Net module for a TCP socket which a TLS socket inherits from. tls.TLSSocket is a subclass of net.Socket. This is a common issue with documentation for a class hierarchy where you don't realize that lots of things are documented in the base class documentation. In that doc, it says this for the end event:

Emitted when the other end of the socket sends a FIN packet, thus ending the readable side of the socket.

By default (allowHalfOpen is false) the socket will send a FIN packet back and destroy its file descriptor once it has written out its pending write queue. However, if allowHalfOpen is set to true, the socket will not automatically end() its writable side, allowing the user to write arbitrary amounts of data. The user must call end() explicitly to close the connection (i.e. sending a FIN packet back).

For the close event, that same doc says this:

Emitted once the socket is fully closed. The argument had_error is a boolean which says if the socket was closed due to a transmission error.

This means that the close event comes after the end event since the socket may be still at least partially open when the end event is received.

So, you will get end when the other side has told you it is no longer accepting data (receipt of FIN packet) and you will get close when the socket is now completely closed.

Answer 2

The tls.TLSSocket class is an instance of the net.Socket class. You can find additional information about the events and methods it has in that documentation (https://nodejs.org/api/net.html#net_class_net_socket). Most likely, net.Socket#end if I had to guess.

Rent Charter Buses Company
READ ALSO
node app instance name when running via pm2 cluster

node app instance name when running via pm2 cluster

I have backend node app, that is run by pm2 in cluster modeI'm running fixed 2 instances

617
Including files not present in repository

Including files not present in repository

This is kind of a general question, I've never had to deploy my own code and I'm not sure how to go about doing this properly

272
SQL Server - Performance issue with concurrent queries

SQL Server - Performance issue with concurrent queries

I am recently facing a troubling issue with SQL ServerI am using a node program to connect and run queries

268
MVC: Should a controller access another controller's model?

MVC: Should a controller access another controller's model?

So I'm working on a MEAN stack where I have four models, and four controllers that handle their data

226