# MQTT protocol violation: Identifier already in use (CONNACK code 2)

- **ID:** `communication/mqtt-publish-identifier-already-in-use`
- **Domain:** communication
- **Category:** protocol_error
- **Verification:** ai_generated
- **Fix Rate:** 90%

## 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.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 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 | — | — |

## Workarounds

1. **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())}"'.** (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())}"'.
   ```
2. **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.** (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.
   ```
3. **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.** (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.
   ```

## Dead Ends

- **Disable the 'allow_multiple_connections' flag in the broker to force single connections** — 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. (90% fail)
- **Restart the broker to clear all active connections** — Restarting is a temporary fix that clears all sessions; the duplicate ID problem reappears when the same client reconnects without changing its ID. (85% fail)
- **Use a static Client Identifier that is hardcoded in the firmware** — Static IDs guarantee collisions if multiple devices run the same firmware; they also make session state management impossible. (95% fail)
