Websocket reconnect

Is there an easier way to reconnect to a websocket, after connection was broken as to restart node red?

1 Like

Depends what you are trying to do. You really haven't given us anything to go on.

Reconnections are a client issue not a server one so we need to know what clients and servers are at play here and how you are connecting.

If node-red is the server and your client is a browser web page served from Node-RED then uibuilder would do all this for you since it includes the Socket.IO libraries and manages the connections for you. But that is only one possible way of course.

I have one pi, which manage a solar system. This one provides an API over websocket. On another Pi, which serves node-red I send ws:// commands to the first pi to pull information of the solar production. In case the battery runs out of energy, the first Pi shuts down and on again, when enough energy is available again. After it is powered on again, the only way to get the connection back to work is, to restart node-red on the second Pi. Direct calls to the API from CL works well, so I think its node-red, especailly the websocket-out or -in node, are not able, to reconnect.

Wow! Now that's an impressive use of a Raspberry Pi!! (Solar System - Wikipedia) :rofl:

But yes, I get what you are saying. A solar power system. Sorry, couldn't resist the dad-joke.

I can't say I've ever tested that but yes, I expect you are right. You would need to detect if a request failed using a catch node and somehow try to reconnect.

However, I'd say that you could do better simply by running Node-RED on the first pi as well, have the websocket connection there and a very simple flow that gets the API data and outputs to MQTT. Run the Mosquitto MQTT broker on the 2nd pi and adjust the node-red flow on that 2nd pi to listen for updates from the 1st.

That way, if the 1st pi looses power, when it restarts, node-red will restart as well and resume outputs to MQTT. That would be a much more resilient solution.

The first Pi is a closed system of the manufacturer, so I dont want to touch it and I do not have user credential on it. So websocket is the only way.

There was another recent thread where someone ended up using a websockets library manually with a function node. That would let you use a websocket client javascript library that supports reconnections instead of the websocket nodes.

If anyone wants to enhance the existing websocket nodes to support this in a backwards compatible pull request I’m sure we would appreciate it.

Did you try setting the Send heartbeat option in the config of websocket in node ?

In my case this fixed a problem reconnecting to a device after power cycle.

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