How to restart 'failed' websocket connection automatically?


I have an issue with a problematic websocket, occasionally it stops sending data and my flows stop - I can get things operational again by 'restarting flows' which appears to completely reset the websocket connection and things resume nicely for another day or two.

I have a node which detects if data stops being received, but how I can I reset the websocket node?

What do you have at the other end of the websocket? Is it a browser? If so, it is much more likely that the browser has stopped not the node.

I'm using the websocket in node, no browser etc...

Hmm, not so easy to trace then. You may have to up the logging level for Node-RED and wade through the logs to see if you can spot anything.

You might also want to startup Node.js with the inspect option - or use VScode's builtin debugger. That should give you an insight to what is happening with the network.

Also, if you are currently using Node-RED <-> Node-RED via websockets, you could switch to UDP or TCP instead maybe. Depends where on your network the other end connection is. Websockets are not always the most stable of connections as far as I can tell and do seem prone to being interrupted by network issues even when everything is on the same device.

Thanks for the reply.

I was really hoping there was a way to restart the connection - or even restart the flows (as this is a quick fix!).

I think that you may be able to restart the flows via the admin API but I don't really use that so I'm not sure.

Taking a quick look at the websocket node, it doesn't include any recovery logic. Though I'm sure that @dceejay would welcome a discussion on that front if you wanted to have a go at designing something. The code doesn't look that complex but I don't know the library so I don't know how easy it would be to add some reconnect logic to the in node.

The websocket nodes use the simple "ws" package rather than the complex does more than just websockets and so is more reliable across WAN's and through firewalls than straight websockets. But it is a pain to use. I use it in uibuilder to give the most robust communications between the client front-end and the Node-RED server. Rightly, the websocket nodes have focused on simplicity.

There is also an existing contrib. node that might help.