aws / aws-iot-device-sdk-embedded-C

SDK for connecting to AWS IoT from a device using embedded C.
MIT License
975 stars 623 forks source link

MQTT Connection configuration guidelines #1817

Closed u93 closed 1 year ago

u93 commented 1 year ago

Hi,

We are looking to use the SDK in places where the network connectivity is unstable, and we are looking to know the best possible parameters for a reliable connection.

We based our application out of the shadow demo application

Specifically looking for proper timeout values for:

Also, what are the proper ways to recover the application using the SDK after receiving the following status from Mqtt_ProcessLoop():

One of the reasons I ask this is because I get these sometimes, and I'm not sure what can be causing it... maybe my application is doing something that is blocking the reception of a package, although I doubt it, but it would be good to know if the timeouts setups can help to improve in that regard.

[ERROR] [MQTT] [core_mqtt.c:780] Unable to receive packet: Timed out in transport recv.
[ERROR] [MQTT] [core_mqtt.c:893] Packet reception failed. ReceivedBytes=8059, ExpectedBytes=22665.
[ERROR] [MQTT] [core_mqtt.c:2192] Exiting process loop due to failure: ErrorStatus=MQTTRecvFailed
[ERROR] [SHADOW_DEVICE] [shadow_helpers.c:887] MQTT_ProcessLoop returned with status = 4

Thanks in advanced!

johnrhen commented 1 year ago

Hello u93,

For unstable connections, the most relevant timeouts will be the values passed to Openssl_Connect() for sendTimeoutMs and recvTimeoutMs. Increasing the value on these timeouts may reduce the occurrence of MQTTRecvFailed errors by giving more time for a packet to arrive before it is considered lost. However, if you are experiencing packet loss, modifying timeouts may not improve your situation significantly. You may be able to recover from the MQTT_ProcessLoop() errors you specified by setting up a retry/reconnect mechanism, but the data loss would still occur.

The timeoutMs for MQTT_ProcessLoop is an implementation detail specifying how long the calling process should repeatedly check for incoming packets/handle KeepAlive/send packets. An application can use a timeoutMs of 0 to do a single iteration and return immediately. Modifying this value shouldn't affect the reliability of your connection.

The timeoutMs in MQTT_Connect() determines how long to wait for the initial CONNACK packet. Modifying this value will only affect the initial connection process, and otherwise should not impact your connection reliability.

I hope this helps - feel free to respond with any additional questions.

johnrhen commented 1 year ago

Feel free to reopen this issue if you have any further questions.