We do it this way:
- onConnect a client sets a lastWill for the topic `APP_ID/CLIENT_ID/status` to `-1` - when the client connected successfully, it sets `APP_ID/CLIENT_ID/status` to `1` - if the client disconnected gracefully, it sets `APP_ID/CLIENT_ID/status` to `0`
All publishes have to be retain=true. We use qos = 0.
This way we know, if a client disconnected gracefully, or failed with a last Will.
Then on the server side, we have another small program acting as a watchdog, which subscribes to the wildcard `APP_ID/#/status`.
This means it gets all status information about the clients. This way a client only has to unsubscribe from `status` gracefully or via lastWill, and our watchdog will take care of cleaning up all used (retained) topics of a client (everything for `APP_ID/CLIENT_ID/#`)
So cleaning up a CLIENT_ID involves publishing a retained empty payload (empty byte array) to the retained `APP_ID/CLIENT_ID/status` . You can only delete topics in mosquitto by publishing empty payload to it.
This way, all new subscribers, which will subscribe after this CLIENT_ID deletion, (i.e. via `APP_ID/#/status`) will not know that this client existed, because it is gone.
All subscribers, which were subscribed during deletion of the CLIENT_ID will still get a message with an empty payload (which they should ignore).
Using the watchdog, we can control, if we want to delete the client immediately, or wait a little bit, if the lastWill was sent. Maybe the client is just flapping and will come back soon, so all other devices should know the client is currently not connected, but it was here a minute ago.
Greetings,
Jan
Hi,
regarding last will, I do it like this: on connection, I have a client publishing a message "online" in topic clientID/status. I set last will to also publish in that topic, but the message is then "offline". Both messages must have retain set to true. A "will" message will be published by the broker in the specified topic, only after it doesn't receive keep alive messages from client.
_______________________________________________ mosquitto-dev mailing list mosquitto-dev@xxxxxxxxxxxTo change your delivery options, retrieve your password, or unsubscribe from this list, visit https://dev.eclipse.org/mailman/listinfo/mosquitto-dev
|