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.

@dceejay sorry to revive an old thread, but we continue to fight this. Currently we restart flows via the NR API, but this causes some delay and means that logic is temporarily down while the restart takes place...

Is there a way we can add a simple reconnect logic to the WS node?

Many thanks in advance.

would really like this feature as well. currently have: nodered > websocket in > connecting to a rhasspy server. when the rhasspy raspberry pi reboots the websocket in not longer works until i restart the node-red instance.

having websocket have reconnect logic would be really useful, or for some way to fix it automatically.

I would use MQTT instead. It has many advantages already "built in" like the re-connecting you are looking for as well as less overhead. What type of data do you transfer?

MQTT works perfect, big thank you! Still using websockets for the TTS (output), this however doesn't seem to hang (so far).