I am currently working on a project,a chat application that uses MQTT where there are 3 clients A, B, C. when every client join they initially subscribe to 2 topic for A its A-B, A-C, while B subscribes to A-B, B-C and c subscribes t0 A-C and B-C. these are the topics the chat messages are sent to and received from. Now i want to add the presence support, to notify if the user is offline or online. Is there any built in mechanism that MQTT broker supports to achieve this objective.
Best How To :
Some broker implementations may provide suitable hooks for connection/disconnection events to enable this, but it isn't a requirement of the protocol for brokers to do so.
It is possible to create a presence aware system using the capabilities of the protocol
You can read more here: https://github.com/mqtt/mqtt.github.io/wiki/presence
In summary, when a client connects, it should define a Last Will message of
0 to be published to the topic
client/[client-id] with the retained flag set. It should then publish a retained message to a topic
client/[client-id] with a value of
This means anyone can subscribe to
client/+ and get the current state of all known clients:
- a message of
1 on topic
client/A means A is currently connected
- a message of
0 on topic
client/A means A is currently disconnected
As an aside, you should consider carefully what topics you want the clients to subscribe to. As you describe it, you have each client explicitly subscribing to every other client's topic. This will not scale very well - think about how you could use wildcards to manage this.