How to restart 'failed' websocket connection automatically?

Hello,

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 socket.io. Socket.io 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).

Did you have any luck with this at node level, other than restarting the flows ?

About a year later, but still...

At your "Websocket in" node, you have to define the url (ws://xxx.xxx.xxx.xx).
At that page is an option "send heartbeat". Set it to 60 sec or so.

It will check if a response is received or not. If not, it wil restart the connection automatically.

So if you already have a configured "websocket in", double click that node and click on the edit button next to the address. (the pencil icon). On that screen you can see the heartbeat option.

I hope this helps you, or other people out there.

1 Like

I don't see this option (Node-RED v2.2.2)


The heartbeat is on the out node not the in.

1 Like

Ah - that would be why I could not find it! I don't use the "out" node, so I think I will have to stay with my current process of restarting the flows via the API :frowning:

When using the "Websocket in" (you are the client), if you choose "connect to" (not 'listen on'), you can enter a heartbeat.

Also, the "Connect to" means: connect to a websocket-server that is listening for new connections)
the "listen on" means: listen for incoming connections.

2 Likes