Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mqtt.MqttClient is not a constructor #524

Open
byoung96 opened this issue Aug 30, 2024 · 13 comments
Open

mqtt.MqttClient is not a constructor #524

byoung96 opened this issue Aug 30, 2024 · 13 comments
Labels
bug This issue is a bug. p2 This is a standard priority issue

Comments

@byoung96
Copy link

Describe the bug

I am trying to setup a connection to AWS IoT from a web application using Mqtt5 and custom auth. Whenever I call client.start() I receive the error mqtt.MqttClient is not a constructor.

Expected Behavior

I would expect that if I had the custom auth configured correctly it would successfully make the connection to mqtt when I call the method client.start().

Current Behavior

image
I get the following error.

Reproduction Steps

This was mainly happening in a React app that is built using Vite. I thought it might be a configuration issue so I cloned this repo and tried the pub_sub_mqtt5 browser sample. Since I don't use the Cognito I switched the config setup to be the custom auth. Below what the createClient was changed to:

function createClient(): mqtt5.Mqtt5Client {
  let wsConfig: iot.MqttConnectCustomAuthConfig = {
    username: "",
    password: Buffer.from("", "base64"),
    tokenKeyName: "",
    tokenValue: "",
    tokenSignature:"",
    authorizerName: "",
  };

  let builder: iot.AwsIotMqtt5ClientConfigBuilder =
    iot.AwsIotMqtt5ClientConfigBuilder.newWebsocketMqttBuilderWithCustomAuth(
      "hostname",
      wsConfig,
    );

  let client: mqtt5.Mqtt5Client = new mqtt5.Mqtt5Client(builder.build());

  client.on("error", (error) => {
    log("Error event: " + error.toString());
  });

  client.on(
    "messageReceived",
    (eventData: mqtt5.MessageReceivedEvent): void => {
      log("Message Received event: " + JSON.stringify(eventData.message));
      if (eventData.message.payload) {
        log("  with payload: " + toUtf8(eventData.message.payload as Buffer));
      }
    },
  );

  client.on("attemptingConnect", (eventData: mqtt5.AttemptingConnectEvent) => {
    log("Attempting Connect event");
  });

  client.on("connectionSuccess", (eventData: mqtt5.ConnectionSuccessEvent) => {
    log("Connection Success event");
    log("Connack: " + JSON.stringify(eventData.connack));
    log("Settings: " + JSON.stringify(eventData.settings));
  });

  client.on("connectionFailure", (eventData: mqtt5.ConnectionFailureEvent) => {
    log("Connection failure event: " + eventData.error.toString());
  });

  client.on("disconnection", (eventData: mqtt5.DisconnectionEvent) => {
    log("Disconnection event: " + eventData.error.toString());
    if (eventData.disconnect !== undefined) {
      log("Disconnect packet: " + JSON.stringify(eventData.disconnect));
    }
  });

  client.on("stopped", (eventData: mqtt5.StoppedEvent) => {
    log("Stopped event");
  });

  return client;
}

I'd also added that in my vite project I tried the proposed solution in issue #474 but that also hasn't worked.

Possible Solution

My best guess as for a solution would be a configuration that needs to be changed. Im sort of at a loss here.

Additional Information/Context

No response

SDK version used

1.21.0

Environment details (OS name and version, etc.)

Sonoma 14.1 on m3 pro

@byoung96 byoung96 added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Aug 30, 2024
@zammitjames
Copy link

This is also happening to me in NextJS or a standard static site.

@JoshuaKirby88
Copy link

Same issue here using the older mqtt.MqttClient() instead of mqtt5.Mqtt5Client() with the latest SDK.
I used to be on 1.19.6 and was working fine so I reverted back to that version which resolved this issue for me, but now I'm encountering a separate issue where connection.connect() which returns Promise never resolves.

I feel like it's unrelated to your authorization, because I had my custom authorization through lambda working fine, but was still seeing both issues.

@dominichadfield
Copy link

Likewise. Static site with react and vite. Only change across the whole stack is upgrading from 1.20.1 to 1.21.0

@Sangammm
Copy link

Sangammm commented Sep 1, 2024

I'm facing the same issue with mqttClient. (not using mqtt5)

I was on version 1.19.5 and it worked till 29th Aug.
Since the launch of version 1.21.0 below code has stopped working.

Code

const mqttConnectionConfig = iot.AwsIotMqttConnectionConfigBuilder.new_default_builder()
  .with_client_id(clientId)
  .with_credentials(region, accessKeyId, secretAccessKey, sessionToken)
  .with_endpoint(endPoint)
  .with_clean_session(witCleanSession)
  .build();

console.log('MQTT ~ mqttConnectionConfig:', mqttConnectionConfig);
const client = new mqtt.MqttClient();
const mqttConnection = client.new_connection(mqttConnectionConfig);

ERROR

mqtt.MqttClient is not a constructor

Other info

Node version 14.18.0
NPM version 6.14.15
Webpack ^5.51.1

@NicoPowers
Copy link

I am also having this issue with SolidStart / SolidJS

@juancapua
Copy link

I'm having the same problem using react and vite

@bretambrose
Copy link
Contributor

This is clearly due to the 5.x upgrade to the mqtt-js dependency. We will look into it tomorrow first thing.

@emog
Copy link

emog commented Sep 3, 2024

I'm having the same problem. Waiting @bretambrose to fix it tomorrow.

@jmklix jmklix added investigating This issue is being investigated and/or work is in progress to resolve the issue. p1 This is a high priority issue and removed needs-triage This issue or PR still needs to be triaged. labels Sep 3, 2024
@thdxr
Copy link

thdxr commented Sep 3, 2024

if you need a reproduction this vite project has the issue: https://github.com/sst/ion/blob/dev/examples/aws-realtime/web/src/main.ts

@bretambrose
Copy link
Contributor

There seems to be some baffling issues with mqtt-js 5.x and webpack. We are likely going to downgrade the CRT dependency and then rollback the upgrade within the CRT.

@byoung96
Copy link
Author

byoung96 commented Sep 3, 2024

There seems to be some baffling issues with mqtt-js 5.x and webpack. We are likely going to downgrade the CRT dependency and then rollback the upgrade within the CRT.

That would be great so that we have atleast something that workable in the meantime.

@bretambrose
Copy link
Contributor

The previous release is workable.

@sfod
Copy link
Contributor

sfod commented Sep 6, 2024

It seems, the issue is in the published MQTT-js package. mqttjs/MQTT.js#1934 and embroider-build/ember-auto-import#608 (comment) provide more details.
There is a workaround provided there, but we need to assess it before applying.

For now, we reverted the MQTT-js version update in the new release: https://github.com/aws/aws-iot-device-sdk-js-v2/releases/tag/v1.21.1

@jmklix jmklix added p2 This is a standard priority issue and removed investigating This issue is being investigated and/or work is in progress to resolve the issue. p1 This is a high priority issue labels Sep 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. p2 This is a standard priority issue
Projects
None yet
Development

No branches or pull requests