Frequent disconnect/reconnect issue with IoT Hub Device.

Peder Dueled 0 Reputation points
2025-05-19T08:36:28.8833333+00:00

I have an IoT Hub on the Standard plan (S1) and i have set up two IoT Hub Devices.
One of the devices is routing data regularly and has a stable connection over MQTT (mqtts). The other one, with the exact same setup just with a different DeviceID, disconnects 2-5 seconds after establishing a connection. I am using MQTTX to test the connection. If I enable auto reconnect, it disconnects and reconnects with the same frequency (2-5 seconds between reconnect and disconnect every time).

If I time it correctly I can send telemetry messages, but the connection still disconnects right after.

Here is my setup for testing in MQTTX:

Host: mqtts://my-iot-hub-name.azure-devices.net
Port: 8883
ClientID: my-iot-hub-device
Username: my-iot-hub-name.azure-devices.net/my-iot-hub-device/?api-version=2018-06-30 (also tried with api-version=2021-04-12)
Password: generated SAS token with 60 minute expiration
SSL/TLS: Enabled
SSL Secure: Enabled
Certificate: CA signed server certificate
MQTT Version: 3.1.1 (also tried with 5.0 and 3.1)
Connect Timeout: 10
Keep Alive: 60
Clean session: Enabled

If you have any questions about my setup or error stack traces I am happy to answer you.

Any help with this issue is greatly appreciated.

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

2 answers

Sort by: Most helpful
  1. Manas Mohanty 5,940 Reputation points Microsoft External Staff Moderator
    2025-05-19T14:34:38.07+00:00

    Hi Peder Dueled

    Sorry for the inconvenience.

    Periodic disconnect might happen due to below reasons.

    • Keep alive interval- If the device doesn’t send a ping request within the keep-alive interval, the IoT Hub will disconnect it - We should increase the keep alive interval to max 1767 / 1.5 = 1177 second - Reference - https://learn.microsoft.com/en-us/azure/iot/iot-mqtt-connect-to-iot-hub#default-keep-alive-time-out
    • Duplicate session is used with same device id - We should use only one device id
    • Connect time out - Please increase time out second (like 20 or 30 second)
    • Please check Devices are enabled on IOT hub.
    • IoT Hub doesn't support QoS 2 messages. If a device application publishes a message with QoS 2, IoT Hub closes the network connection.

    Please check Device side logs to find out Device Specific issues

    Please check below sample code

    https://learn.microsoft.com/en-us/azure/iot/iot-mqtt-connect-to-iot-hub#samples-using-mqtt-without-an-azure-iot-device-sdk

    Other Scenarios

    https://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718127

    Reference used - https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-troubleshoot-connectivity#mqtt-device-disconnect-behavior-with-azure-iot-sdks

    Hope it helps

    Thank you.

    0 comments No comments

  2. Sander van de Velde | MVP 36,766 Reputation points MVP Volunteer Moderator
    2025-05-19T22:30:59.31+00:00

    Hello Peder Dueled

    welcome to this moderated Azure community forum.

    Although MQTTX is a great tool for testing MQTT brokers, it's not the favourite tool for testing an IoT Hub.

    This is because the IoT Hub only has limited supports for MQTT, just as the transport protocol with a limited set of topics for supporting device registration, device twins, direct methods, etc.

    Please use the Azure IoT Hub device SDK to connect to the IoT hub or use the documented way to connect over MQTT only. Here is (an older) C# example on how to connect.


    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.