You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
doc clarification between ws.close() and ws.terminate() #1142
This may not tell the complete story.
I found that ws.close() may still execute signal handlers 30 seconds after close() if the network connection is unavailable, while ws.terminate() did not show this behaviour.
During a change of network (e.g. from wired to wireless), when I create a new WebSocket() and registered all signal handlers (onopen/onclose/onerror), I was struggling that the new connection went up, but was consistently receiving 1006/onclose events from the previous socket, which is confusing, but also correct behaviour.
My question is highly related and dont want to open an issue, super appreciated if you can clarify something, I noticed in the source code that error triggers a close
Are error and close always triggered together
Are there situations where error can be triggered without close and vice-versa?
I am trying to call a reconnect method when the connection errors out unless I explicitly disconnected it, having some problems with things happening twice
Not always, there might be buffered data and until all data is read the 'close' event is not emitted. You always get a 'close' event after an 'error' event.
Are there situations where error can be triggered without close and vice-versa?
'close' can be emitted without 'error' (clean close).
I am trying to call a reconnect method when the connection errors out unless I explicitly disconnected it, having some problems with things happening twice
Use the 'close' event and an heartbeat system based ping/pong frame where the connection is terminated if no ping is received within an expected amount of time.
Reacted by Czar Pino
Reacted by slidenerd, Casey Duncan, McSneaky, Michael Sobrepera, CodeJuggernaut, ohbeomho and vyu-charted
I looked at the code and I gather it emits close if the state is CONNECTING but not otherwise. In the code below abortHandshake() does emit close but then you have this._socket.destroy() which doesn't trickle up to the WebSocket instance ... what am I missing?
terminate() {
if (this.readyState === WebSocket.CLOSED) return;
if (this.readyState === WebSocket.CONNECTING) {
const msg = 'WebSocket was closed before the connection was established';
return abortHandshake(this, this._req, msg);
}
if (this._socket) {
this.readyState = WebSocket.CLOSING;
this._socket.destroy();
}
}
@lpinca Does terminate inform the other side of what is happening? I am trying to write a test where the client disconnects un-grafully (to test my ping pong handler and kicker) and I'm wondering if terminate can be used for this, from what I see it triggers the close event on the server side (which is not what I want).
Not explicitly by sending a frame but it does because it closes the TCP connection. It's not trivial to write that test on localhost or a local network.
@lpinca Ah I see.. One more question: Is there a way to forcibly refuse the sending of a pong from the client in this WS library if the server sends a ping (essentially breaking the WS protocol), this behavior would trigger my ping pong kicker in that case?
Use ws.terminate() to shut down the connection immediately.
This sounds like it's just "faster" than .close(). Could you maybe edit your post to advise more caution?
What it really does is not a clean shutdown, but rather it simply drops the connection. One should only do this in case of an error. There is no closing handshake, neither on the websocket nor the tcp level, the other end of the connection will be oblivious of the dropped connection. (They will only run into a tcp timeout after (if!) attempting to send something).
Activity
lpinca commentedon Jun 14, 2017
Use
ws.close()when you want to close the connection gracefully and/or want to use a specific close code/reason. This implements the close handshake.Use
ws.terminate()to shut down the connection immediately.brubbel commentedon Oct 27, 2017
This may not tell the complete story.
I found that
ws.close()may still execute signal handlers 30 seconds after close() if the network connection is unavailable, whilews.terminate()did not show this behaviour.During a change of network (e.g. from wired to wireless), when I create a new WebSocket() and registered all signal handlers (onopen/onclose/onerror), I was struggling that the new connection went up, but was consistently receiving 1006/onclose events from the previous socket, which is confusing, but also correct behaviour.
Please also refer to #891
Work around ws library bug
slidenerd commentedon Oct 19, 2018
My question is highly related and dont want to open an issue, super appreciated if you can clarify something, I noticed in the source code that error triggers a close
Are error and close always triggered together
Are there situations where error can be triggered without close and vice-versa?
I am trying to call a reconnect method when the connection errors out unless I explicitly disconnected it, having some problems with things happening twice
lpinca commentedon Oct 19, 2018
Not always, there might be buffered data and until all data is read the
'close'event is not emitted. You always get a'close'event after an'error'event.'close'can be emitted without'error'(clean close).Use the
'close'event and an heartbeat system based ping/pong frame where the connection is terminated if no ping is received within an expected amount of time.aleqx commentedon Feb 14, 2019
Is the 'close' event emitted if I call
.terminate()?lpinca commentedon Feb 14, 2019
@aleqx yes.
aleqx commentedon Feb 14, 2019
I looked at the code and I gather it emits
closeif the state is CONNECTING but not otherwise. In the code belowabortHandshake()does emitclosebut then you havethis._socket.destroy()which doesn't trickle up to the WebSocket instance ... what am I missing?lpinca commentedon Feb 14, 2019
@aleqx, see
ws/lib/websocket.js
Lines 773 to 808 in b45e912
aleqx commentedon Feb 14, 2019
That's what I missed! Many thanks.
basickarl commentedon Jun 24, 2020
@lpinca Does terminate inform the other side of what is happening? I am trying to write a test where the client disconnects un-grafully (to test my ping pong handler and kicker) and I'm wondering if terminate can be used for this, from what I see it triggers the close event on the server side (which is not what I want).
lpinca commentedon Jun 24, 2020
Not explicitly by sending a frame but it does because it closes the TCP connection. It's not trivial to write that test on localhost or a local network.
basickarl commentedon Jun 24, 2020
@lpinca Ah I see.. One more question: Is there a way to forcibly refuse the sending of a pong from the client in this WS library if the server sends a ping (essentially breaking the WS protocol), this behavior would trigger my ping pong kicker in that case?
lpinca commentedon Jun 24, 2020
You can but not in an official way.
It can break at any time.
lpinca commentedon Jun 24, 2020
Another way would be to override
ws.pong()with a noop function.basickarl commentedon Jun 24, 2020
@lpinca Thank you ever so much dude! I tried both and they both worked (I'll use the latter one).
Esqarrouth commentedon Feb 16, 2021
Is there a way to terminate from the client side?
bergus commentedon Oct 15, 2022
@lpinca
This sounds like it's just "faster" than
.close(). Could you maybe edit your post to advise more caution?What it really does is not a clean shutdown, but rather it simply drops the connection. One should only do this in case of an error. There is no closing handshake, neither on the websocket nor the tcp level, the other end of the connection will be oblivious of the dropped connection. (They will only run into a tcp timeout after (if!) attempting to send something).
More details: https://stackoverflow.com/a/74081838/1048572?when-is-it-preferable-to-use-ws-terminate-vs-ws-close