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

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

Client disconnects due to PINGREQ not running #1805

Closed twlawrenceko closed 2 years ago

twlawrenceko commented 2 years ago

Hello,

We are using the example code from here and with the removal of returnStatus = DisconnectMqttSession(); we expect the PINGREQ should continue to avoid client disconnect due to MQTT_KEEP_ALIVE_TIMEOUT.

Here is the activity we see in AWS IoT:

{
  "clientId": "esp32",
  "timestamp": xx,
  "eventType": "disconnected",
  "clientInitiatedDisconnect": false,
  "sessionIdentifier": "xx",
  "principalIdentifier": "xx",
  "disconnectReason": "MQTT_KEEP_ALIVE_TIMEOUT",
  "versionNumber": 4
}

Thank you

ravibhagavandas commented 2 years ago

Hello, Apologies for delay in response. To keep the connection alive we need to send PINGREQ at regular intervals defined by keep alive timeout. To send keep alive packets you can either call

  1. MQTT_ProcessLoop which will handle PINGREQ as well as receive any incoming packets from the broker in background.
  2. MQTT_Ping which sends an explicit PINGREQ.

If application is idle and have no other processing to do, I would suggest calling MQTT_ProcessLoop with a timeout parameter so that it can handle incoming packets as well.

ravibhagavandas commented 2 years ago

Hope this clarifies your question. Please feel free to open an issue if you have further questions.