Skip to content

AWS IoT MQTT Client Error Handling Improvement Needed #543

Open
@GuilhermeScherner

Description

Describe the bug

The MQTT client error handler in our application is not providing sufficient error type information when connection issues occur, particularly with WebSocket connections and credential expiration. This makes it difficult to implement proper error handling and recovery strategies.

const currentCredentials = await Auth.currentCredentials();
const clientId = guffo-${Math.floor(Math.random() * 100000 + 1)}-${Date.now()};

  const client = new mqtt.MqttClient();
  const config = iot.AwsIotMqttConnectionConfigBuilder.new_builder_for_websocket()
    .with_clean_session(true)
    .with_client_id(clientId)
    .with_endpoint(process.env.REACT_APP_AWS_ENDPOINT || '')
    .with_credentials(
      process.env.REACT_APP_AWS_COGNITO_REGION || '',
      currentCredentials.accessKeyId,
      currentCredentials.secretAccessKey,
      currentCredentials.sessionToken,
    )
    .with_keep_alive_seconds(60)
    .build();

  const mqttClient = client.new_connection(config);

So when I try to catch errors with this code

  mqttClient.on('error', async (error) => {
      console.log('Connection error:', error);
      if (navigator.onLine) await resetConnection();
    });

    if (!notSubscribeRef.current) {
      mqttClient.on('interrupt', async () => {
        console.log('Connection interrupted');
        if (navigator.onLine) await resetConnection();
      });

      mqttClient.on('disconnect', async () => {
        console.log('Disconnected from MQTT');
        if (navigator.onLine) await resetConnection();
      });

      mqttClient.on('connection_failure', async () => {
        console.log('Connection failure');
        setTimeout(resetConnection, RECONNECT_DELAY);
      });
    }

Expected Behavior

The error handler should:

  1. Provide detailed error type information to distinguish between different failure scenarios
  2. Include specific error codes or types for common failures like:
    • Credential expiration
    • WebSocket connection failures
    • Network connectivity issues
    • Authentication failures
  3. Allow implementing specific handling strategies based on error types

Current Behavior

Currently, when connection issues occur:

  1. The error event handler receives a generic error object without specific error type information
  2. We cannot distinguish between different types of failures (credential expiration, network issues, etc.)
  3. The error handler triggers a generic reconnection attempt without being able to handle specific cases differently
  4. No specific error information is provided when credentials expire or when WebSocket connection fails

Reproduction Steps

Initiate an MQTT websocket connection using cognito and wait for the connection to close on error.

Possible Solution

No response

Additional Information/Context

No response

SDK version used

SDK Version: aws-iot-device-sdk-js-v2 1.17.0

Environment details (OS name and version, etc.)

Platform: Browser (React application)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    feature-requestA feature should be added or improved.p3This is a minor priority issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions