domoticz / Reacticz

Reacticz dashboard
Apache License 2.0
34 stars 15 forks source link

Connection to MQTT broker failed #38

Open wewa00 opened 3 years ago

wewa00 commented 3 years ago

Hello, Reacticz is not able to connect to the MQTT broker. But I do not understand why. The url and the data used for authentication are valid. I can use the same data and connect via mqtt-spy via websockets to the MQTT broker. Mosquitto MQTT v3.1/v3.1.1 Broker is used as MQTT broker.

Any idea what is failing here?

Best regards wewa

secator76 commented 3 years ago

Hello, same problem. I think, it's since the Mosquitto 2.0.4 update.

with this command: sudo tail -f /var/log/mosquitto/mosquitto.log i've this error: 1610030766: Client 'unknown' closed its connection. when i connect with REACTICZ.

t0mg commented 3 years ago

Hey, yeah that seems linked to the MQTT update indeed.

Unfortunately I'm no longer able to maintain or debug reacticz*, so I can't help you with this issue.

Here are a few things you could try:

(* if anyone is interested in taking over it would be nice !)

t0mg commented 3 years ago

PS: Reacticz should be rewritten anyway if the MQTT loopback is removed, see https://github.com/domoticz/domoticz/issues/4035

wewa00 commented 3 years ago

The issue following. The browser blocks connection to the MQTT server, because I access domoticz via https. But the browser detects the mqtt connection as unsecure (non https) and blocks it therefore.

secator76 commented 3 years ago

The issue following. The browser blocks connection to the MQTT server, because I access domoticz via https. But the browser detects the mqtt connection as unsecure (non https) and blocks it therefore.

same for me

wewa00 commented 3 years ago

And another additional information: It is only possible to connect to MQTT Websocket port, if TLS is disabled there.

UPDATE: In case you want to connect to a TLS secured MQTT Websocket port, you need to use wss:// instead of ws:// for the MQTT Broker URL (see https://stackoverflow.com/a/46559376/1528248).

wewa00 commented 3 years ago

@t0mg regarding support and contribution, is there any channel where we could exchange (like discord)?

t0mg commented 3 years ago

Nope no Discord sorry, but Github issues should be enough :)

wewa00 commented 3 years ago

OK, you saw my pull request (#40) already. I think we should continue using MQTT because that is working good and it does not need lots of changes. So what stays open in my eyes because of https://github.com/domoticz/domoticz/issues/4035 is following.

But I could need some introduction here in how this program is structured because I am not used to JavaScript developing nore to node.js.

PS: Another issue which should be addressed is, that the MQTT und Domoticz credentials are stored in clear text in the Browsers Local Storage. I think those should be encrypted by some protected key for security reasons.

t0mg commented 3 years ago

Not sure why a polling timer would be necessary if Domoticz properly publishes state changes? It kind of defeats the purpose of using MQTT/websockets too.

It's a react app, which typically means that the data flow is unidirectional. A Reacticz dashboard is a list of user selected devices (with their position in the grid). Reacticz calls Domoticz to fetch these devices, tries to figure out their type and calls the proper widget for each. In the widgets, we define how to render the device state and what the available user actions are. When a user presses a button, we either send an MQTT message or a JSON command. The app monitors incoming messages at a global level, and updates the state of all widgets, which in turn re-render to reflect the changes. In other words, tapping a button in a widget does not update the UI directly, it only sends the message to Domoticz. The UI is updated by state changes at the app level that cascade down to each relevant widget. This way we are guaranteed to be consistent with the state of Domoticz, and thanks to MQTT over websockets, it's faster than polling solutions. We do use polling for some cases that are not supported by the MQTT implementation (eg IIRC, scenes dont trigger MQTT updates for all devices).

Reacticz was bootstrapped with Create React App which has good documentation for getting started.

Regarding credentials, you are correct: they are stored in clear text and that's not ideal. We could put these in a cookie instead with an expiry date but I'm guessing most users would not be pleased with the change, and it would still be clear text. I don't see how these credentials could be locally and securely encrypted in a reversible way (we need them in clear text to connect to Domoticz and the MQTT broker). Instead there should likely be a token-based auth with a cookie, but I don't know if token-based solutions are available for both MQTT and Domoticz, maybe that's worth exploring if you feel strongly about it ?

wewa00 commented 3 years ago

The issue here with Domoticz is, that Domoticz is not really notifying over every value change (e.g. for temperature sensor, ...). I noticed this in a private project already where I use Domoticz as an UI for a heationg control based on python and MQTT.

I solved it there by polling the relevant values. And I think it would be good to do the same for Reacticz.

In fact it is not really a polling, you simply trigger Domoticz to broadcast the value and react then, because you subscribed the Domoticz/out topic.

So is there already some timer callback where this "value update trigger functionality" could be implemented?