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.
By clicking “Sign up for GitHub”, you agree to our
terms of service
and
privacy statement
. We’ll occasionally send you account related emails.
Already on GitHub?
Sign in
to your account
I would like to understand the exact difference between ws.close() and ws.terminate().
I don't find the doc very clear, about it because as I tested them, they behave exacly the same. Both trigger "close" event on the other side if open.
so when to use one instead of the other?
thank you.
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.
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.
Please also refer to
#891
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
Are error and close always triggered together
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.
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).
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).
More details: https://stackoverflow.com/a/74081838/1048572?when-is-it-preferable-to-use-ws-terminate-vs-ws-close