What is the client id and where do I find it?

Written by Jon Börjesson

We get a lot of questions about what the client id is and where it can be found. This short article will hopefully help you during your MQTT journey.

The short answer is that the client id is chosen by you. Often it’s not even necessary to specify a client id because the client library will use a randomly generated if it’s not given explicitly.

However, it can be good to chose client id wisely to make it easier to setup ACL rules and to make the broker log easier to read. It’s also good to have client ids one remembers, e.g. Johns-phone or customer-12345.

When a client subscribes to a topic, the subscription is associated with the client id and not the connection or username. When a messages is published to a topic, the broker will iterate over all subscriptions matching the topic and look up if any connection is active with the associated client id. If a connections i found, the message will be published to the currently connected device. If the subscription is done with QoS 1 or 2, and the message are published with at least QoS 1 the message will be retained in the broker and delivered next time a client with the right client id connects.

What happens when you are reusing the client id?

When a device connects and there is another connection with the same client id, the latter will be disconnected. This is because the broker will assume that the old connection has died and devices is just reconnecting.