Skip to content

Last Will and Testament (LWT) ​

The Last Will is a message a client leaves with the broker when it connects. If the client disappears without saying goodbye, the broker publishes the will for it, so every subscriber learns that the device is gone.

How it works ​

  1. Connect with a will. In the CONNECT packet the client sets a will topic, payload, QoS and retain flag, for example …/cnc-01/status → {"state":"offline"}, retained.
  2. Stay connected. The broker keeps the will as long as the connection is alive.
  3. Disappear. If the connection ends without a DISCONNECT packet, the broker publishes the will to its topic.
How the connection endsWill published?
Power loss, crash, network drop, socket closedYes
No packet within 1.5 × the keep-alive timeYes
The client sends DISCONNECT (a normal shutdown)No

On ConnectMQ the will is published as a normal message of the client: it goes to the client's own organization and is subject to its ACL rules.

The online / offline pattern ​

js
import mqtt from 'mqtt'

const status = 'acme/istanbul/assembly/line1/cnc-01/status'

const client = mqtt.connect('mqtts://broker.connectmq.com:8883', {
  clientId: `${process.env.CMQ_CLIENT_ID}_cnc-01`,
  username: process.env.CMQ_USERNAME,
  password: process.env.CMQ_PASSWORD,
  will: { topic: status, payload: '{"state":"offline"}', qos: 1, retain: true },
})

client.on('connect', () => {
  // Overwrite the retained "offline" as soon as we are back
  client.publish(status, '{"state":"online"}', { qos: 1, retain: true })
})

Because both messages are retained, the status topic always shows the current state, also to subscribers that join later. Before a planned shutdown, publish offline yourself, then disconnect.

In the console and with Edge ​

  • An alert rule on the status topic (for example {state} == "offline") raises an alert when a device disappears.
  • The broker also publishes every client's connection state on $SYS/broker/connection/<client ID>/state (system topics).
  • Sparkplug B uses the will for NDEATH messages; ConnectMQ converts them to …/death topics (Sparkplug B).

Fully managed MQTT broker for connected devices.