# 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:** 1
**Showing post:** 2

<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…

---

_[View the full topic](https://discourse.nodered.org/t/how-to-disconnect-a-websocket-programmatically/57637)._
