Mongoose Embedded Web Server MQTT Last Will Question

If you are asking a question, please follow this template:

  1. My goal is: Have my MQTT Broker publish a last will and testament message to specified topic.
  2. My actions are: I have set the topic and message (with qos1) on the client before connecting to the broker, connect to the broker, subscribe with a second client to the set Last Will Topic (qos1 as well), cut power to the client, wait…
  3. The result I see is: The LWT message is not published from the broker and the disconnect is never acknowledged.
  4. My expectation & question is: My expectation is to have the last will message published when disconnect happens.

I am maybe looking for an example of the MQTT Last Will and Testament feature. I seem to be doing something incorrectly and I’m not sure if I’m missing a step on the broker side.

Do I need to write code to handle an abrupt disconnect of an MQTT Client on the broker and publish the last will message accordingly, or will the broker take care of that situation already?

If anyone has an example or blog post on this topic it would be greatly appreciated.


If I understand correctly and you are using Mongoose-OS MQTT client and some broker, then, assuming you set the parameters correctly, your broker will send the LWT as soon as it detects there is a disconnection.
How can it detect a disconnection if you disappear from the eath without a trace ? When either the MQTT keepalive timer expires or the TCP timer expires (enough times) and the session is closed. That may take a long time depending on your broker and your configuration.
The shorter the keepalive time, the faster the detection. Most providers will limit and/or charge this traffic

Hello and thank you for your reply. I appreciate it a lot.

I should have been more clear in my first post, sorry about that.

I have a Mongoose Embedded Web Server based MQTT Broker that is running on an esp32. The clients are also running on esp32s. I set the last will parameters in the configuration in one and connect, and subscribe to the expected last will topic with the other.

You’re answer, however, may still very well be the answer to my issue I just wanted to paint a clearer picture for you.

Also, I do wait till the TCP connection times out after some time, and still notice the LWT is not fired.

I’m guessing I am supposed to be handling when the keep alive timer expires programatically broker side and publishing manually if it is not done automagically.

I seem to understand that you are writing code for a MQTT broker.
Well, I’ve never written code for a broker so I can’t be of much help here. In my understanding of communications systems, MQTT will either detect the disconnection by timeout waiting for a response to a PING request (or several of them, don’t have the standard at hand right now, nor time to dig on it) or indication of the lower level (TCP) of a disconnection. TCP, in turn, will detect a disconnection by reception of proper handshake or error signals, or repeated timeouts of either keepalives (if configured) or outstanding sent segments (missing acknowledges). Under some conditions TCP might take hours to detect that, so typically a PING timeout of the desired order is used to handle that at the application level (MQTT).
I can’t help you on the specifics, I don’t know where you are standing, I know brokers and I know clients, don’t know the innards of a specific broker nor what it does or doesn’t do. If you are doing what I think you are doing, you should ask in the Mongoose forum, this one is Mongoose-OS’ and you have less chance to find a Mongoose guru.

Thank you for the input. Again I really appreciate your time.

I was actually directed to this forum by a Mongoose representative I worked with while purchasing a license, and I havent been able to find the forum for the Mongoose Embedded Web Server.

I have also since read that the Last Will has not been implemented for what I want to do (however I am getting the configured lwt parameters in the CONNECT event on the Broker), so I will probably need to come up with another solution.

Oh, perhaps I’m wrong and there is no specific Mongoose forum…
Anyway, if there is no support for LWT actions you’ll have to do it yourself.
I would listen for a disconnect event and a PING timeout (perhaps you’ll have to implement the PING yourself ?). When either one is true, you send the LWT message on behalf of the client. That is more or less what a broker should do based on the specs.
Maybe you could check how Mosquitto does it.

Thanks for the information!

There is a ping event that the broker will react to. I would think this comes with a Session or Connection ID… How would the broker be able to keep track of connected devices, and what topics they are subscribed to otherwise? If that’s the case I could capture that and keep track of the keep alive timer sent in the CONNECT event.