MQTT protocol violation: Identifier already in use (CONNACK code 2)
ID: communication/mqtt-publish-identifier-already-in-use
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| Mosquitto 2.0.18 | active | — | — | — |
| EMQX 5.3.0 | active | — | — | — |
| HiveMQ 4.28.0 | active | — | — | — |
| VerneMQ 1.13.0 | active | — | — | — |
| AWS IoT Core MQTT endpoint | active | — | — | — |
Root Cause
An MQTT client attempts to connect with a Client Identifier that is already in use by another active connection to the same broker, violating the uniqueness requirement of MQTT v3.1.1 section 3.1.3 and MQTT v5.0 section 3.1.3.
generic中文
MQTT客户端尝试使用已被同一代理上另一个活动连接使用的客户端标识符进行连接,违反了MQTT v3.1.1第3.1.3节和MQTT v5.0第3.1.3节中的唯一性要求。
Official Documentation
https://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718030Workarounds
-
95% success Generate a unique Client Identifier per connection using a combination of device MAC address and Unix timestamp: in Python, use 'client_id = f"device-{mac}-{int(time.time())}"'.
Generate a unique Client Identifier per connection using a combination of device MAC address and Unix timestamp: in Python, use 'client_id = f"device-{mac}-{int(time.time())}"'. -
85% success Enable 'clean_session' (MQTT v3.1.1) or 'session_expiry_interval=0' (MQTT v5) on the client to force the broker to discard previous session state when a duplicate ID is detected.
Enable 'clean_session' (MQTT v3.1.1) or 'session_expiry_interval=0' (MQTT v5) on the client to force the broker to discard previous session state when a duplicate ID is detected.
-
70% success Configure the broker to allow multiple connections with the same Client ID (e.g., Mosquitto: 'allow_duplicate_client_ids true'), but only if session isolation is not required.
Configure the broker to allow multiple connections with the same Client ID (e.g., Mosquitto: 'allow_duplicate_client_ids true'), but only if session isolation is not required.
中文步骤
Generate a unique Client Identifier per connection using a combination of device MAC address and Unix timestamp: in Python, use 'client_id = f"device-{mac}-{int(time.time())}"'.Enable 'clean_session' (MQTT v3.1.1) or 'session_expiry_interval=0' (MQTT v5) on the client to force the broker to discard previous session state when a duplicate ID is detected.
Configure the broker to allow multiple connections with the same Client ID (e.g., Mosquitto: 'allow_duplicate_client_ids true'), but only if session isolation is not required.
Dead Ends
Common approaches that don't work:
-
Disable the 'allow_multiple_connections' flag in the broker to force single connections
90% fail
This flag controls whether multiple connections with the same ID are allowed; disabling it only enforces the error, it doesn't fix the client's duplicate ID.
-
Restart the broker to clear all active connections
85% fail
Restarting is a temporary fix that clears all sessions; the duplicate ID problem reappears when the same client reconnects without changing its ID.
-
Use a static Client Identifier that is hardcoded in the firmware
95% fail
Static IDs guarantee collisions if multiple devices run the same firmware; they also make session state management impossible.