typescriptmqttiotaws-iotmqtt.js

WebSocket connection to <AWS IoT Url> failed: EMPTY


I am working with this library for my AWS IoT MQTT topics. However, I've run into an issue when trying to connect to the presigned-url of the following format:

wss://<endpoint>.iot.us-west-2.amazonaws.com/mqtt?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=<credential>%2Fus-west-2%2Fiotdevicegateway%2Faws4_request&X-Amz-Date=<date>&X-Amz-Expires=86400&X-Amz-SignedHeaders=host&X-Amz-Signature=<signature>&X-Amz-Security-Token=<security-token>

Here is the sample code I've used for this test:

import * as mqtt from 'mqtt';

try {
  const options: Partial<IClientOptions> = {
    will: {
      topic: 'hello',
      payload: JSON.stringify({ clientId: '1', username: 'sean' }),
      qos: 1,
      retain: true
    },
    port: 443,
    clientId: '1', 
    username: 'sean'
  };
const client = mqtt.connect(
  request.url,
  options);

console.log(client);

client.on('connect', function() {
  client.subscribe('hello', function (err) {
    if (!err) {
      client.publish('presence', 'Hello mqtt')
    }
  });
});

client.on('error', (err) => {
  console.log('error', err);
  client.end()
});

} catch (error) {
  console.log(error);
}

Unfortunately, when I boot up my webpage, I am receiving the following repeated error in the Chrome dev tools:

WebSocket connection to <AWS IoT Url> failed:

Usually, there is an actual error message displayed after the '... failed:' string. In the code, I am seeing a nop function that is logging the error. Just curious what errors usually cause the nop function to be called and how could I can begin testing why my url is failing to connect?


Solution

  • Found out that my AWS IoT IAM credentials were invalid so I was unable to properly connect to the cloud. If the developers have time, it would be useful if the connect function would notify that this might be the problem. The current functionality of displaying an empty error message made it difficult to actually debug the problem 🤔