# Websocket connected and disconnected

**URL:** <https://discourse.nodered.org/t/websocket-connected-and-disconnected/85206>\
**Category:** General\
**Created:** [5 February 2024 03:09 UTC](https://discourse.nodered.org/t/websocket-connected-and-disconnected/85206 "2024-02-05T03:09:28Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dr\_Phil](https://avatars.discourse-cdn.com/v4/letter/d/c2a13f/32.png) [@Dr\_Phil](https://discourse.nodered.org/u/Dr_Phil)\
**Post date:** [5 February 2024 03:09 UTC](https://discourse.nodered.org/t/websocket-connected-and-disconnected/85206/1 "2024-02-05T03:09:28Z")

</div>

I'm using Websocket in and out and they work fine. I need to know if a Client is connected or disconnected and whilst this is shown there is no msg available. There is an alternative node:

> **[node-red-contrib-websocket](https://flows.nodered.org/node/node-red-contrib-websocket)**
>
> A web-socket with connected and disconnected events support.

that provides msg.connected = true/false. However, there is a conflict shown when I try and install this.  
Is there a way this can be installed please?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [5 February 2024 04:08 UTC](https://discourse.nodered.org/t/websocket-connected-and-disconnected/85206/2 "2024-02-05T04:08:34Z")

</div>

You may need to expand on  
_There is a conflict shown when I try and install this_.

---

<div class="post-metadata">

**Author:** ![Dr\_Phil](https://avatars.discourse-cdn.com/v4/letter/d/c2a13f/32.png) [@Dr\_Phil](https://discourse.nodered.org/u/Dr_Phil)\
**Post date:** [5 February 2024 05:00 UTC](https://discourse.nodered.org/t/websocket-connected-and-disconnected/85206/3 "2024-02-05T05:00:30Z")

</div>

I select Manage Palette, Install, then search for websocket.

node-red-contrib-websocket is displayed but on the right a box that contains 'conflict' is shown. If I place my mouse on this box then "_This module cannot be installed as it includes a node type that has already been installed. Conflicts with node-red_" is displayed.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [5 February 2024 07:50 UTC](https://discourse.nodered.org/t/websocket-connected-and-disconnected/85206/4 "2024-02-05T07:50:19Z")

</div>

Have you tried using the status node it should report the connect/disconnect of the websocket nodes.

---

<div class="post-metadata">

**Author:** ![Dr\_Phil](https://avatars.discourse-cdn.com/v4/letter/d/c2a13f/32.png) [@Dr\_Phil](https://discourse.nodered.org/u/Dr_Phil)\
**Post date:** [5 February 2024 08:52 UTC](https://discourse.nodered.org/t/websocket-connected-and-disconnected/85206/5 "2024-02-05T08:52:28Z")

</div>

Thank you. The status node does report connect but not disconnect since the websocket does not indicate when it does not have a client connected to it unless a message is sent to the client. Then a disconnect is received after about 15 seconds.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [5 February 2024 09:06 UTC](https://discourse.nodered.org/t/websocket-connected-and-disconnected/85206/6 "2024-02-05T09:06:58Z")

</div>

You are mistaken , it reports connect and disconnect after timeout, no messages need to be sent.

 ![chrome_screenshot_5 Feb 2024 09_04_43 GMT](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/7/d78828ae52713a9ce0d813c16801c8c6e8c249fc.png)

---

<div class="post-metadata">

**Author:** ![Dr\_Phil](https://avatars.discourse-cdn.com/v4/letter/d/c2a13f/32.png) [@Dr\_Phil](https://discourse.nodered.org/u/Dr_Phil)\
**Post date:** [5 February 2024 09:40 UTC](https://discourse.nodered.org/t/websocket-connected-and-disconnected/85206/7 "2024-02-05T09:40:17Z")

</div>

I've left it disconnected for 10 minutes and its still showing connected. Do I need to set a timeout, if so how do I do that please?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [5 February 2024 10:03 UTC](https://discourse.nodered.org/t/websocket-connected-and-disconnected/85206/8 "2024-02-05T10:03:32Z")

</div>

How do you have it set up?  
How are you disconnecting it?  
The timeout is built into the websocket node.

---

<div class="post-metadata">

**Author:** ![Dr\_Phil](https://avatars.discourse-cdn.com/v4/letter/d/c2a13f/32.png) [@Dr\_Phil](https://discourse.nodered.org/u/Dr_Phil)\
**Post date:** [5 February 2024 10:36 UTC](https://discourse.nodered.org/t/websocket-connected-and-disconnected/85206/9 "2024-02-05T10:36:48Z")

</div>

It’s set up as a Websocket server. The client runs on an ESP32. All data sent to and from the client works fine and when the client first connects then I get the connect signal.  
I remove the power from the ESP32 to disconnect it.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [5 February 2024 11:14 UTC](https://discourse.nodered.org/t/websocket-connected-and-disconnected/85206/10 "2024-02-05T11:14:18Z")

</div>

1. What version of NR and node.js are you running? you can get this from the NR startup log (@E1cid you too)

2. this node hasn't been updated in almost 8 years...

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [5 February 2024 12:14 UTC](https://discourse.nodered.org/t/websocket-connected-and-disconnected/85206/11 "2024-02-05T12:14:46Z")

</div>

Latest 3.1.3 nodejs 20.2.0  
@Dr_Phil export your flow please

[How to import/export a flow](https://nodered.org/docs/user-guide/editor/workspace/import-export)

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote `````)

````auto
``` 
   code goes here 
```

````

You can edit and correct your post by clicking the pencil ✏ icon.

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<div class="post-metadata">

**Author:** ![Dr\_Phil](https://avatars.discourse-cdn.com/v4/letter/d/c2a13f/32.png) [@Dr\_Phil](https://discourse.nodered.org/u/Dr_Phil)\
**Post date:** [6 February 2024 01:47 UTC](https://discourse.nodered.org/t/websocket-connected-and-disconnected/85206/12 "2024-02-06T01:47:21Z")

</div>

I'm using Node-Red v3.1.0 and NT 10.0.19045. My code is as follows:

```auto
[
    {
        "id": "2157535e595135c6",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "83ab13540db76534",
        "type": "websocket out",
        "z": "2157535e595135c6",
        "name": "WS Server Out",
        "server": "83ae3e14c71f11ab",
        "client": "",
        "x": 540,
        "y": 340,
        "wires": []
    },
    {
        "id": "f304cc6cbb939f5c",
        "type": "websocket in",
        "z": "2157535e595135c6",
        "name": "WS Server In",
        "server": "83ae3e14c71f11ab",
        "client": "",
        "x": 250,
        "y": 460,
        "wires": [
            [
                "b2aa114626afeace"
            ]
        ]
    },
    {
        "id": "b2aa114626afeace",
        "type": "debug",
        "z": "2157535e595135c6",
        "name": "debug 2",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 500,
        "y": 460,
        "wires": []
    },
    {
        "id": "7ee01a5d1c656be8",
        "type": "comment",
        "z": "2157535e595135c6",
        "name": "WS Server /ws/test",
        "info": "",
        "x": 450,
        "y": 280,
        "wires": []
    },
    {
        "id": "3c377ddf7c022da2",
        "type": "inject",
        "z": "2157535e595135c6",
        "name": "status 1",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "{\"status\":1}",
        "payloadType": "json",
        "x": 250,
        "y": 320,
        "wires": [
            [
                "83ab13540db76534"
            ]
        ]
    },
    {
        "id": "9ea2af6953b005c5",
        "type": "inject",
        "z": "2157535e595135c6",
        "name": "status 0",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "{\"status\":0}",
        "payloadType": "json",
        "x": 250,
        "y": 380,
        "wires": [
            [
                "83ab13540db76534"
            ]
        ]
    },
    {
        "id": "7bf565bdf93cdf88",
        "type": "status",
        "z": "2157535e595135c6",
        "name": "Check Conect status",
        "scope": [
            "83ab13540db76534"
        ],
        "x": 790,
        "y": 340,
        "wires": [
            [
                "8546c3f74650a9f7"
            ]
        ]
    },
    {
        "id": "8546c3f74650a9f7",
        "type": "debug",
        "z": "2157535e595135c6",
        "name": "debug 4",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 1000,
        "y": 340,
        "wires": []
    },
    {
        "id": "8b42b490a6d66ff1",
        "type": "status",
        "z": "2157535e595135c6",
        "name": "Check Conect status",
        "scope": [
            "f304cc6cbb939f5c"
        ],
        "x": 790,
        "y": 460,
        "wires": [
            [
                "2837a776f1458c39"
            ]
        ]
    },
    {
        "id": "2837a776f1458c39",
        "type": "debug",
        "z": "2157535e595135c6",
        "name": "debug 5",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 1000,
        "y": 460,
        "wires": []
    },
    {
        "id": "83ae3e14c71f11ab",
        "type": "websocket-listener",
        "path": "/ws/test",
        "wholemsg": "false"
    }
]

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [6 February 2024 09:06 UTC](https://discourse.nodered.org/t/websocket-connected-and-disconnected/85206/13 "2024-02-06T09:06:59Z")

</div>

Not seeing an issue in your flow,.  
What is your nodejs version?

Does your esp send constant messages to the websocket in node, you could possible set up some sort of heartbeat as a workaround.

---

<div class="post-metadata">

**Author:** ![Dr\_Phil](https://avatars.discourse-cdn.com/v4/letter/d/c2a13f/32.png) [@Dr\_Phil](https://discourse.nodered.org/u/Dr_Phil)\
**Post date:** [6 February 2024 09:28 UTC](https://discourse.nodered.org/t/websocket-connected-and-disconnected/85206/14 "2024-02-06T09:28:57Z")

</div>

My Node.js version is v21.1.0

I'm periodically sending a ping from the ESP32 and checking I get a pong back, which if all connected I do. But this is not detected by node.red so perhaps sending heatbeat data could be used.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [6 February 2024 17:10 UTC](https://discourse.nodered.org/t/websocket-connected-and-disconnected/85206/15 "2024-02-06T17:10:40Z")

</div>

> [@Dr\_Phil](#):
>
> My Node.js version is v21.1.0

That is not a supported version, only LTS versions are supported. I suggest downgrading to v18 which is the current recommended version.

---

<div class="post-metadata">

**Author:** ![Dr\_Phil](https://avatars.discourse-cdn.com/v4/letter/d/c2a13f/32.png) [@Dr\_Phil](https://discourse.nodered.org/u/Dr_Phil)\
**Post date:** [8 February 2024 06:06 UTC](https://discourse.nodered.org/t/websocket-connected-and-disconnected/85206/16 "2024-02-08T06:06:16Z")

</div>

I've downgraded to v18 and loaded the latest node-red. Manage Palette now shows that the version of websocket installed supports msg.connected = true/false and that it is in use. It also shows 4 nodes are available:  
websocket-in  
websocket-out  
websocket-listener  
websocket-client

However, msg.connected does not provide any data, even though status node shows connected, and I can't locate all 4 nodes.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [8 February 2024 08:05 UTC](https://discourse.nodered.org/t/websocket-connected-and-disconnected/85206/17 "2024-02-08T08:05:00Z")

</div>

> [@Dr\_Phil](#):
>
> websocket-in  
> websocket-out

Are the nodes that you can grab from the node palette and add in your flows while

> [@Dr\_Phil](#):
>
> websocket-listener  
> websocket-client

are configuration nodes that are used when you configure the other two nodes. They do not display in the flow

---

<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:** [8 May 2024 08:05 UTC](https://discourse.nodered.org/t/websocket-connected-and-disconnected/85206/18 "2024-05-08T08:05:27Z")

</div>

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