- Newest
- Most votes
- Most comments
The default behavior of the AWS IoT MQTT connection builder for reconnection is designed to be resilient, but it may not guarantee indefinite reconnection attempts under all circumstances.
By default, when you don't set specific reconnection timeout values, the SDK uses internal default values. These defaults are not explicitly stated in the provided sources, but they are likely to be reasonable values that work for most use cases.
The reconnection mechanism works as follows:
- When a disconnection occurs, the SDK will attempt to reconnect.
- The time between reconnection attempts starts at the minimum timeout and doubles after each unsuccessful attempt, up to the maximum timeout.
- This exponential backoff strategy helps to avoid overwhelming the server with rapid reconnection attempts.
However, there are circumstances where the connection might not be re-established:
- If there are persistent network issues or the IoT Core endpoint is unreachable.
- If the device's credentials are no longer valid.
- If there are security group or firewall rules blocking the connection.
To make the connection more resilient and attempt to reconnect indefinitely:
-
Set appropriate minimum and maximum reconnection timeouts:
builder.withReconnectTimeoutSecs(1, 60);This example sets a minimum of 1 second and a maximum of 60 seconds between reconnection attempts.
-
Implement a custom reconnection strategy in your application logic. This could involve:
- Monitoring the connection state
- Implementing your own retry mechanism with longer intervals
- Logging disconnection events for troubleshooting
-
Consider implementing a keep-alive mechanism to detect disconnections early:
builder.withKeepAliveSeconds(30);This sets a 30-second keep-alive interval to help detect disconnections more quickly.
-
Handle disconnection events in your application and initiate a new connection if needed:
connection.onDisconnection((disconnectionData) -> { // Log the disconnection // Attempt to create a new connection });
Remember that while these strategies can improve resilience, they may not guarantee a connection under all circumstances, especially in cases of persistent network issues or invalid credentials. It's important to implement proper error handling and logging in your application to manage these scenarios effectively.
Sources
https://docs.aws.amazon.com/embedded-csdk/202012.00/lib-ref/libraries/standard/coreMQTT/docs/doxygen/output/html/mqtt_timeouts.html
https://docs.aws.amazon.com/embedded-csdk/202103.00/lib-ref/libraries/standard/coreMQTT/docs/doxygen/output/html/mqtt_timeouts.html
TcpKeepAliveConfiguration.Builder (AWS SDK for Java - 2.21.29)
Relevant content
- asked 5 years ago
- asked 7 years ago
- AWS OFFICIALUpdated 8 months ago
- AWS OFFICIALUpdated 3 years ago
- AWS OFFICIALUpdated a year ago

This does not clarify if it will give up reconnecting under certain circumstances. I was hoping it never gives up but in practice I'm seeing the contrary.