Publishing an LWT message when the client is disconnected
NOV62 opened this issue · 7 comments
Hello there!
I really liked Your project. I have a question: can I configure the broker to publish a message in the LWT topic when the client is disconnected? Can I use the "client disconnected" event in the script?
No, currently you cannot set an LWT topic as a client. You have to do this, when you connect to an external broker and there is no parameter so far.
There was a static event in https://github.com/martin-ger/esp_mqtt/blob/master/user/user_main.c#L916, but it is disabled now. If you recompile it, you can enable it. If I find some time, I will add config variables for that.
Thanks for the answer. Unfortunately, I have a very poor understanding of programming, use Arduino IDE a little and don't know how to recompile your code. The uMQTTBroker library does not suit me because of its very limited capabilities.
As far as I understand, if the showmqtt command displays a list of connected clients, then this information could be used in a script, for example:
"on client_disconnect do
publish LWT / "$ this_client-id" disconnect [retained] ".
Why did you disable the above event?
We have to distinguish two cases:
- This esp_mqtt software as a client to another broker: here we have no LWT topic so far as explained above.
- Other clients connected to this esp_mqtt broker: in this case the client can define its LWT topic on connection and the broker software will automatically publish it to the remaining clients according to the MQTT specification, when the connection is lost. No further action required here. It is up to the client to set the LWT topic. This works.
The rule you are specifying above will publish a topic to the clients of this esp_mqtt broker, when the software disconnects from the other external broker. You can do that, but it is not a classic LWT, as you usuall even don't have a local broker and a connection to another external broker at once.
Hmm, this should work without further config.
I'm sorry, I need to check again. I expected the LWT disconnect message to be sent to an external broker, but it is clear that this is not happening. This means you need to create a rule for relaying LWT messages from other clients for local clients. It's a crutch, but it should work.
OK, the broker is working fine. The cause of ESPEasy firmware problems is extremely unstable WiFi operation, constant reconnections cause data loss and lead to the crash of the broker. After flashing the clients, everything worked.
Thanks!