I have a question about when to publish the will message

I look at the source code and find that there seems to be no mechanism for ensuring the sending of will messages when the server is down. It looks like that it only takes into account the case where the client disconnects unexpectedly. Is that true?

Hello @scutcr7 ,

Great to see your interest in MQTT, please be welcome in our community!

Yes, the behaviour you describe is correct according to the MQTT Specification:

When does a broker send the LWT message?

According to the MQTT 3.1.1 specification, the broker must distribute the LWT of a client in the following situations:

  • The broker detects an I/O error or network failure.
  • The client fails to communicate within the defined Keep Alive period.
  • The client does not send a DISCONNECT packet before it closes the network connection.
  • The broker closes the network connection because of a protocol error.

None of the conditions listed above is “the broker is shut down”, hence no LWT is expected to be distributed.

Note, when you manually disconnect a client session (that has LWT) from the broker (via the Control Center → Clients), then the broker will confirm it with you, whether you wish to send the LWT or not. See the screenshot:

If you have any further doubt, please feel free to share your concern with me.

Kind regards,
Dasha from HiveMQ Team

1 Like

One more important thing to add:

The commercial HiveMQ editions offer high availability through clustering to ensure LWTs are also received, if one of the brokers crashes.

Kind regards,
Dasha from HiveMQ Team

1 Like