How to get websocket msg on (dis)connection?

Hello all,

I'm using Node-RED as server to manage websockets sessions.
One of my biggest problems is to detect when a session is disconnecting. With classic NodeJS we have an event onClose. But i don't know how to do it in Node-RED.
When a client is disconnecting i would like to have a msg with session and a payload object like "disconnected".

Did you know a way to do it please ?

Have a nice day.

Are you using the websocket nodes? I'm not sure that they support a disconnect event. I think they use raw websockets and I'm not even sure how much you can trust any disconnect events since the client might simply go away without shutting down. So you might be reliant on a ping/pong message - again, not sure if that is available in raw websockets.

Sorry, sounding rather vague there. :slight_smile: I mostly work with

You can try the status node.


According to this comment node-red/22-websocket.js at master · node-red/node-red · GitHub there are no server-side heartbeats, thus no timeouts.

If the client disconnects cleanly, you should get a close message emitted by the ws node: node-red/22-websocket.js at master · node-red/node-red · GitHub

It works !!! Thank you !
With status node you receive message on connected and disconnect event with the websocket session.
Now i can easely control my differents clients websocket sessions.

1 Like

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