2
votes

Problem: If everything in MQTT is pubsub but it feels like for Last Will and Testament (LWT) to work there would have to be Long polling from some end?

Question: In MQTT, How does broker know when a client goes off line? And what's the flow on LWT notifying the broker, is it long polling?

last will test documentation

Last Will And Testament MQTT clients can register a custom “last will and testament” message to be sent by the broker if they disconnect. These messages can be used to signal to subscribers when a device disconnects.

2

2 Answers

4
votes

MQTT has a keep alive mechanism (which is essentially a heartbeat). When a client connects, the keep alive value for that specific client can be specified. If the client is idle for one and a half the time specified and does not send any heartbeat or any other MQTT message, the broker assumes the TCP connection is broken (= half-open).

The HiveMQ MQTT Essentials Blog Series has in-depth articles of the LWT and keep-alive mechanisms:

1
votes

The MQTT protocol has a keep alive time that is set at connection time. If there is no messages published by the client in that time the client will send a pong packet to the server. If the server does not get a publish or a ping in the time it marks the client as disconnected and delivers the LWT message to any other clients subscribed to the relevant topic.