0

I am using below code to connect to MQTT server

MqttConnectOptions options = new MqttConnectOptions();
options.setCleanSession(true);
options.setKeepAliveInterval(Constants.CLOUD_KEEP_CONN_ALIVE);
byte[] payload = String.valueOf(0).getBytes((Charset.defaultCharset());
options.setWill("willTopic", payload, 0, true);
mAndroidAsyncClient.connect(options, this);

Once connected I get a callback . Now my question is how to debug in server , that setWill is working using mosquitto_sub .

I am using setWill to know client presence.

To confirm setWil is working I wanted to debug from server by subscribing to willtopic and get logs .

I tried connecting and disconnecting many times but there is message sent over will topic.

Command I used on server is

mosquitto_sub -t appTopic --will-topic willTopic

It will be helpful if anyone share the command or way to debug Mqtt setWill.

I tried referring to How to Find Connected MQTT Client Details . But it did not workout for me.

Community
  • 1
  • 1
Rahul Patil
  • 2,707
  • 2
  • 21
  • 32

1 Answers1

0

The only real way to test that the LWT (Last Will & Testement) message gets sent is to make the broker send it.

To do this you will have to cause a none clean shutdown of the client, given this is on android the easiest way would be to run the app in the emulator, let it connect then kill the emulator (You have to do this because last time I tried to get the emulator to disable all network activity I discovered it just sent the Network Down broadcasts but left the network up). Once the keep alive period expires and the broker notices the client has gone it should publish the LWT message and you should be able to see this with mosquitto_sub.

There may be ways to query the broker for a given LWT message but this would be totally dependant on the broker you are using. I'm not aware of any brokers that make this easy, but there may be something under the '$SYS/#' topic tree on brokers that support that interface.

EDIT:

Also when using mosquitto_sub you should just be using the -t not --will-topic. --will-topic is the topic that mosquitto_sub would publish it's own LWT on if it was disconnected. You can have as many -t options as you need, and -v will print the topic name before the message body so you can tell them apart

mosquitto_sub -v -t appTopic -t willTopic
Community
  • 1
  • 1
hardillb
  • 54,545
  • 11
  • 67
  • 105