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 1
.
This means anyone can subscribe to client/+
and get the current state of all known clients:
- a message of
1
on topicclient/A
means A is currently connected - a message of
0
on topicclient/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.