Issue with MQTT reconnect to Azure IoT hub, device unable to reconnect/publish

Ben Hoffman 0 Reputation points
2025-03-03T13:27:40.8733333+00:00

Having an issue where the client device is unable to connect back after a network disruption.

  1. Device registers/provisions with DPS successfully
  2. Device generates a SAS token
  3. Device connects to IoT hub with the token successfully
  4. Device subscribes to messages successfully, and receives messages
  5. Device publishes messages successfully, seen in IoT Hub Explorer
  6. We disconnect Wifi, wait for 1.5 x keepalive, see disconnect callback on the client from the MQTT library (Paho C++ v1.4.1, using Paho C v1.3.13), we see the disconnect event in the IoT Hub Explorer on the cloud side.
  7. Device continues to queue publish messages (QOS 1)
  8. We reconnect Wifi
  9. Device reconnects successfully with the same connection string (client ID) to the IoT hub (and the SAS token has not expired)
  10. Device receives subscribed messages
  11. Device publishes a message, we see the message in IoT Hub explorer on the cloud side, and the PUBACK shows on the device side
  12. IoT hub disconnects the device right after the first publish, we see error resultType 400000 with the following content:

{"statusCode":"400","errorMessage":"","deviceId":"E444T03658LDSB","trackingId":"064F8FAD6FC04B3BB099FEA941D617E7-G2:-TimeStamp:2025-03-01T09:32:18.832955579Z","sdkVersion":"Unknown","authType":"{"scope":"device","type":"sas","issuer":"iothub"}","maskedIpAddress":"--.---.---.XXX","protocol":"Mqtt"}

When we run the same scenario against Mosquitto MQTT hosted in the cloud the whole scenario works fine.

We are trying to figure out why the reconnect does not work as we expect, is there an IoT Hub configuration or something we need to do differently on the device around reconnect or publish to make this work?

Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,249 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Sampath 750 Reputation points Microsoft External Staff
    2025-03-04T16:12:51.8366667+00:00

    Hello @Ben Hoffman,

    You're correct that Azure IoT Hub only allows one session per device identity at a time. If a new connection is made with the same device identity while another session is still active, the previous session is forcibly disconnected.

    Thus, if you reconnect with the same identity, the previous session will be disconnected. To resume a session and retain messages, set clean_session = false and reconnect with the same identity. Note that you cannot resume a session with a different identity, because sessions are tied to specific devices.

    Creating a new session requires setting clean_session = true, which resets all previous subscriptions and messages.

    To test message persistence across disconnections, disconnect at 1.5× Keepalive, ensure clean_session = false, and use QoS 1 for guaranteed delivery.

    For more details, refer to this MSDOC for the Azure IoT Client MQTT State Machine.

    Alternatively, if you need multiple endpoints connected to the Azure IoT Hub, you can either use unique device IDs per endpoint or use AMQP device multiplexing. Please note that there are currently bugs in this feature in azure-iot-sdk-c.

    Hope this helps!

    If you found this answer helpful, please click Accept Answer and consider upvoting it /click yes.

    accept

    If you have any further questions, please click Comment.

    0 comments No comments

  2. Sander van de Velde | MVP 35,336 Reputation points MVP
    2025-03-05T17:59:35.0666667+00:00

    Hello @Ben Hoffman,

    welcome to this moderated Azure community forum.

    Check out the answer that @Sampath provided regarding the single session per device.

    Keep in mind the Azure IoT is not designed as an MQTT broker.

    The IoT Hub is a cloud gateway with a registry for device registrations. It support the MQTT protocol with a limited set of topics, a alternative device connectivity strategy, etc.

    If you want to communicate using a vanilla MQTT broker, honouring the V3.1,1 and V5 MQTT protocol, please check out the Azure EventGrid Namespace support for MQTT.

    This is a public cloud based MQTT broker supporting the MQTT protocol.

    It's not a replacement for the IoT Hub but offers separate use cases like device-2-device communication.

    Check this blog post on how to connect and use all features.


    If the response helped, do "Accept Answer". If it doesn't work, please let us know the progress. All community members with similar issues will benefit by doing so. Your contribution is highly appreciated.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.