Establish MQTT connection between a web app and IoT core

0

Hi,

I got a problem when I would like to establish MQTT connection between a web app and IoT core. The web app is built in Angular 13 and I would like to use aws-iot-device-sdk-js-v2 and aws-sdk-js-v3 to establish the MQTT connection.

I had tried with aws-iot-device-sdk-js and aws-sdk-js-v2 to establish the connection and it worked fine.

This time I would like to use the both new sdk, and there are some problems.

1. About the aws-iot-device-sdk-js-v2

I imported the mqtt module and created a client. But I can't setup the credentials, certifiacte and private key, and the credentials_provider. It seems like the library downloaded by npm install is different from what their document descreibes. The example code is not working as well.

The following is my codes:

import { mqtt } from "aws-iot-device-sdk-v2";
const mqttclient = new mqtt.MqttClient();
mqttconfig.clean_session = true;
mqttconfig.client_id = "XingWebApp";
mqttconfig.host_name = 'a3a4e8898qc8uh-ats.iot.us-east-1.amazonaws.com';
mqttconfig.keep_alive = 30;
mqttconfig.port = 8333;
mqttconfig.socket_options
mqttconfig.tls_ctx    
mqttconfig.use_websocket

No "mqttconfig.credentials" can be set up and no clue about how to set up "mqttconfig.socket_options" and "mqttconfig.tls_ctx".

2. About aws-sdk-js-v3

In aws-sdk-js-v2, it is clear to get the accessKeyId, secretAccessKey and sessionToken.

But I can't find the example of aws-sdk-js-v3 about getting above inforamtion. I am using the library, "@aws-sdk/credential-providers"

In the developer guide, the credentials are always got inside the declaration of a client.

The following is the [example code](https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/loading-browser-credentials-cognito.html#:~:text=const%20REGION%20%3D%20AWS_REGION-,const%20s3Client%20%3D%20new%20S3Client(%7B,-region%3A%20REGION%2C):

const s3Client = new S3Client({
  region: REGION,
  credentials: fromCognitoIdentityPool({
    clientConfig: { region: REGION }, // Configure the underlying CognitoIdentityClient.
    identityPoolId: 'IDENTITY_POOL_ID',
    logins: {
            // Optional tokens, used for authenticated login.
        },
  })
});

When I would like to use "fromCoginitoIdentityPool" independently, I got error.

The following is my code:

import { fromCognitoIdentityPool, CognitoIdentityCredentialProvider } from "@aws-sdk/credential-providers";
let mqttconfig: CognitoIdentityCredentialProvider;
mqttconfig = fromCognitoIdentity ({
        clientConfig: { region: Region }, // Configure the underlying CognitoIdentityClient.
        identityId: 'us-east-1:cba13714-5f3a-4a73-81a4-8f7ccf9c35e1',
        logins: LoginData,
})

Above is all about my issue and the references I found. If any information of this issue, I will be really grateful.

1 Answer
0

Hi,

Which issue are you encountering with the following example? https://github.com/aws/aws-iot-device-sdk-js-v2/blob/main/samples/browser/pub_sub/index.ts

I have tried it and it works fine for me.

Cheers,

Massimiliano

AWS
EXPERT
answered 2 years ago
  • Hi,

    From line 81 to line 96.

    More detailes:

    1. In the example, AwsIotMqttConnectionConfigBuilder has a method called "new_builder_for_websocket". But there is no such method when I used AwsIotMqttConnectionConfigBuilder, "new_websocket_builder" is the method I had and is most closest to it. The file path of AwsIotMqttConnectionConfigBuilder: .\node_modules\aws-crt\dist\native\aws_iot.d.ts

    2. To create a provider for ".with_credential_provider", I need auth.CredentialsProvider but CredentialsProvider can't be found in auth module.

    Regards, Raven

  • Hi Raven,

    I see. The issue is that you are using the wrong version of the library, probably because your project is not setup correctly. The right path for aws_iot.d.ts should be .\node_modules\aws-crt\dist.browser\browser\aws_iot.d.ts.

    The aws-crt-js library has 2 implementations: one for Node and one for the Browser and they have different methods. Make sure your project uses the right library. Check how the sample is setup and replicate that in your own project.

    Massimiliano

  • Hi Massimiliano,

    Thank you for the reply.

    To complement my case, I login the web app by AWS Cognito user pool first, and get the token from the session of Cognito.

    After that, I used token and identity pool id to get the credential from Cognito indentity pool.

    At first, I use npm install to download the sdk, and actually I download every library to my Angular project in the same way.

    I had tried to install the sdk in other ways.

  • I am confused with theses situation as well

    1. The errors also occur at compilation and browser when I would like import auth of aws-iot-device-sdk-v2.

    2. Besides the MQTT conenction, another important question for me is how to get the accessKeyId, secretAccessKey and sessionToken thru @aws-sdk/credentials-provider (aws-sdk-js-v3).

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions