# How to disconnect a websocket programmatically?

**URL:** <https://discourse.nodered.org/t/how-to-disconnect-a-websocket-programmatically/57637>\
**Category:** General\
**Created:** [2 February 2022 12:06 UTC](https://discourse.nodered.org/t/how-to-disconnect-a-websocket-programmatically/57637 "2022-02-02T12:06:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rozek](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rozek/32/48594_2.png) [@rozek](https://discourse.nodered.org/u/rozek)\
**Post date:** [2 February 2022 12:06 UTC](https://discourse.nodered.org/t/how-to-disconnect-a-websocket-programmatically/57637/1 "2022-02-02T12:06:44Z")

</div>

Well,

normally, one wants a "websocket out" node to connect to a server in order to send a message and a "websocket in" node to connect as well in order to receive the response.

But, how can one programmatically shut down that connection as soon as all messages have been sent and all responses received?

Changing the flow and redeploying it is not an option - the disconnect shut be done by the flow itself.

Is there a solution?

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [2 February 2022 13:14 UTC](https://discourse.nodered.org/t/how-to-disconnect-a-websocket-programmatically/57637/2 "2022-02-02T13:14:25Z")

</div>

> [@rozek](#):
>
> how can one programmatically shut down that connection as soon as all messages have been sent and all responses received?

the point of websocket is to stay open .. otherwise you use http requests 😉

there was a recent forum thread asking the same but i dont think there is a way to close the connection with the existing nodes.

Here is that post with a code alternative

> [@Dynamically Enable/Disable WebSocket node](https://discourse.nodered.org/t/dynamically-enable-disable-websocket-node/55638/2):
>
> Hello .. i think the core Websocket nodes connect and stay connected and i didnt see a way to force a disconnect. You could load the ws library using the functionExternalModules option in a Function node as a workaround in order to have better control. I dont know much about websockets but here's a flow to demostrate it using an online open test server. (in this example when a msg with the expected data is received .. we force a disconnect ) [{"id":"6ff8ddd581cb52e0","type":"function","z":"5…

---

<div class="post-metadata">

**Author:** ![rozek](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rozek/32/48594_2.png) [@rozek](https://discourse.nodered.org/u/rozek)\
**Post date:** [2 February 2022 14:00 UTC](https://discourse.nodered.org/t/how-to-disconnect-a-websocket-programmatically/57637/3 "2022-02-02T14:00:19Z")

</div>

Thank you very much for your response!

Let me quote [MDN](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/close):

> The **`WebSocket.close()`** method closes the [`WebSocket`](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) connection or connection attempt, if any. If the connection is already `CLOSED` , this method does nothing.

There is much between single HTTP request-response messages and "everlasting" WebSocket connections but it might not be easy to map features such as

- the `connection` event on the server side,
- `WebSocket.bufferedAmount` and
- `WebSocket.close()`

to nodes which are easy to be used...

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [2 February 2022 14:34 UTC](https://discourse.nodered.org/t/how-to-disconnect-a-websocket-programmatically/57637/4 "2022-02-02T14:34:18Z")

</div>

The close method is for clients.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [2 February 2022 15:13 UTC](https://discourse.nodered.org/t/how-to-disconnect-a-websocket-programmatically/57637/5 "2022-02-02T15:13:52Z")

</div>

For a simple websocket server both the client and the server have the same rights to close the connection. So it looks like you can issue a .close method from the server.

[WebSocket (javascript.info)](https://javascript.info/websocket#connection-close)

---

<div class="post-metadata">

**Author:** ![rozek](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rozek/32/48594_2.png) [@rozek](https://discourse.nodered.org/u/rozek)\
**Post date:** [2 February 2022 15:20 UTC](https://discourse.nodered.org/t/how-to-disconnect-a-websocket-programmatically/57637/6 "2022-02-02T15:20:48Z")

</div>

That's a really interesting reference! Thank you!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [3 April 2022 15:21 UTC](https://discourse.nodered.org/t/how-to-disconnect-a-websocket-programmatically/57637/7 "2022-04-03T15:21:30Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
