ws
ws copied to clipboard
No way to detect half-closed websockets
Is there an existing issue for this?
- [X] I've searched for any related issues and avoided creating a duplicate issue.
Description
Is there any way to detect when the remote websocket client has half-closed the connection?
I would like to drop the connection upon this happening but I don't seem to find a way to detect this.
Thank you.
ws version
1.0.34
Node.js Version
v18.13.0
System
Linux Debian 12
Expected result
Some kind of on('end') handler.
Actual result
No on('end') (or similar) handler to be found in the docs or issues.
Attachments
No response
No, there isn't. You can use the 'conclude'
event of the Receiver
but that is an internal only event. It is emitted when a peer receives a close frame.
websocket._receiver.on('conclude', handler);
You shouldn't close the connection when this event is emitted because the closing handshake is not yet complete. The connection should be closed only after the close frame has been sent and received.
Thank you for your reply.
Would it be correct to use the 'conclude' event to set a timer and if, say, 10 seconds later the connection is still not closed, terminate() it?
The library forcibly closes the connection if it is not closed properly 30 seconds after calling websocket.close()
(which is called when the 'conclude'
event is emitted). See also https://github.com/websockets/ws?tab=readme-ov-file#how-to-detect-and-close-broken-connections.
I'm closing this as answered.