Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting close of client connect in node.js

Tags:

node.js

I am using node.js for long held connections. I want to know how I can detect when the client connection closes (maybe when the user closes the tab). How do I specify a callback for that?

like image 871
karter Avatar asked Oct 17 '10 14:10

karter


People also ask

How do I close a NodeJS connection?

The server. close() method stops the HTTP server from accepting new connections.

How do I run NodeJS app forever when console is closed?

js application locally after closing the terminal or Application, to run the nodeJS application permanently. We use NPM modules such as forever or PM2 to ensure that a given script runs continuously. NPM is a Default Package manager for Node.

What does Response end do NodeJS?

end() function is used to end the response process. This method actually comes from the Node core, specifically the response. end() method of HTTP.


1 Answers

Answering my own question - You can detect close of client connection by doing:

http.createServer(function(request, response) {    
    request.connection.addListener('close', function () {
        // callback is fired when connection closed (e.g. closing the browser)
    });
}
like image 162
karter Avatar answered Sep 23 '22 09:09

karter