In MQTT it is the client who initiates the connection with a CONNECT message.

MQTT Connect Package parameters

The first field of the packet is the clientId:

The client identifier (short ClientId) is an identifier of each MQTT client connecting to a MQTT broker. As the word identifier already suggests, it should be unique per broker. The broker uses it for identifying the client and the current state of the client. (Image and quote are taken from from here.)

Now let's say I have two clients, client X and Y in the following situation.

  1. Broker launched, no clients yet.
  2. X succesfully connects to the broker with client-1 id, username is X.
  3. Now, Y tries to connect using client-1 as id, username is Y.

What will happen?

  1. Based on the clientId, the broker will think that X performs a repeated connection attempt which is abnormal behavior.
  2. Nothing extraordinary will happen. Y connects successfully as it uses a different username.
  3. Nothing extraordinary will happen. The broker will reject Y connection attempt as the given clientId is already in use.

1 Answer 1


If the clientid is the same, in MQTT, the spec says you must consider them to be the same client! Probably Y should be connected using the Id and X should be disconnected.

This part is from the documentation:

If validation is successful the Server performs the following steps.

  1. If the ClientId represents a Client already connected to the Server then the Server MUST disconnect the existing Client [MQTT-3.1.4-2].

  2. The Server MUST perform the processing of CleanSession that is described in section [MQTT-3.1.4-3].

  3. The Server MUST acknowledge the CONNECT Packet with a CONNACK Packet containing a zero return code [MQTT-3.1.4-4].

  4. Start message delivery and keep alive monitoring.

Look this documentation for more details.

  • 2
    This seems to be the correct answer, but it might be helpful to link and quote the relevant part of the specification (the key part is "If the ClientId represents a Client already connected to the Server then the Server MUST disconnect the existing Client")
    – Aurora0001
    Dec 29, 2016 at 9:57
  • 3
    I have overlooked that part somehow, but it is surprising. A harmful client can force the broker to disconnect other clients then? Dec 29, 2016 at 10:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.