24

I am using express with node.js for a http server. I store the response object so I can stream events down to the client on that channel. Is there a way to detect when the client disconnects? When I kill my client I can still write to the response object without getting any kind of exception/error. It looks like the service keeps the underlying tcp channel open as long as I keep writing to the response object.

2
  • 4
    Have you considered using socket.io Commented Jul 4, 2011 at 15:27
  • res.connection.destroyed Commented Jan 12, 2020 at 12:42

1 Answer 1

55
req.on("close", function() {
  // request closed unexpectedly
});

req.on("end", function() {
  // request ended normally
});
Sign up to request clarification or add additional context in comments.

6 Comments

nice this is new for me also :)
Note: 'close' can fire after 'end', but not vice versa. I'm confused. How can close be emitter after end? I can only imagine it vice versa. User dropped connection, but for example request to mysql already sent.
both not happening when we switch off wifi. client showing disconnected, but server still connected giving unavailable-id in peerjs server
@PhaneendraCharyuluKanduri i'm in the same boat now, close triggers if the browser or the tab is closed but if there is an unexpected interruption then it is not triggered. wondering is there any handler that we can leverage.
On node 12 close event is fired in the end anyways
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.