0

I have the below code -->

var last_will = new Paho.MQTT.Message("last message");
last_will.destinationName = "Bridge123";
client = new Paho.MQTT.Client("broker.mqttdashboard.com", Number("8000"), "AX123");
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
client.connect({onSuccess:onConnect} , {willMessage:last_will});

When i disconnect the client i expect a last will message being sent to the topic i have created .. Am using Paho 's mqtt version -3.1 .. Websockets are getting created fine but i do not see the last will message ...

Can anyone guide here ?

Adding the bigger picture :

I have a Python script p gathering current on / off status of a IOT device in the local environment and publishing to a topic "IOT1" over mqtt . I do not want the python script always running to get status from the IOT device as it overloads the device .. To solve this i am in need of finding active clients for "IOT1" topic so that i run or pause the thread sending requests to the IOT device in the local environment .. Is there a way other than the last will message to know this ?

2 Answers2

2

Last Will and Testament messages are only published if client does not disconnect cleanly.

If you close the connection gracefully it will not be sent.

Only when the server fails to receive a message or ping packet in the time out period will the server send the message.

hardillb
  • 54,545
  • 11
  • 67
  • 105
0

willMessage should be the property of the first object. See below code snippet.

client.connect({onSuccess:onConnect, willMessage:last_will});