Omitting the ClientId
when connecting will make the broker generate a unique id for this client (if permitted). From the MQTT specification:
A Server MAY allow a Client to supply a ClientId that has a length of zero bytes, however if it does so the Server MUST treat this as a special case and assign a unique ClientId to that Client. It MUST then process the CONNECT packet as if the Client had provided that unique ClientId (Oasis MQTT)
Using an empty ClientId
comes along with the condition that it has to be a CleanSession
- otherwise the connection will be refused:
If the Client supplies a zero-byte ClientId, the Client MUST also set CleanSession to 1
(because the ClientId
is essential for the broker to persist and resume on a session)
Since the broker stores the client's subscriptions (among other things) within the session, you always have to re-subscribe all topics when using an empty ClientId
and thereby with CleanSession=1