Skip to content

Instantly share code, notes, and snippets.

@HQarroum
Last active May 4, 2020 15:05
Show Gist options
  • Save HQarroum/4e8eaaf9b45fbbfe945b675f18ee3c0f to your computer and use it in GitHub Desktop.
Save HQarroum/4e8eaaf9b45fbbfe945b675f18ee3c0f to your computer and use it in GitHub Desktop.
Using the AWS IoT SDK with ALPN extensions to connect over MQTTS on port 443
const client = require('aws-iot-device-sdk');
// The options object to provision the MQTT client with.
// Update values between chevrons with the appropriate values.
const opts = {
host: "<aws-iot-endpoint>",
keyPath: "<path-to-private-key>",
certPath: "<path-to-device-certificate>",
caPath: "<path-to-root-ca>",
// We are specifying that we want to connect on the
// port 443 of the AWS IoT Core broker.
port: 443,
// Enables the `x-amzn-mqtt-ca` protocol on the TLS connection.
ALPNProtocols: ["x-amzn-mqtt-ca"]
};
// Initiating the connection.
const mqttClient = client.device(opts);
// Listening for a connection event.
mqttClient.on('connect', () => console.log(`[+] Successfully connected to AWS IoT over the port ${opts.port}!`));
// Listening for an error event.
mqttClient.on('error', (err) => console.error('[!] An error occured during the connection', err));
@HQarroum
Copy link
Author

HQarroum commented Feb 23, 2020

I've just tested on my side and it works fine.

Note that this example relies on the aws-iot-device-sdk-js v1 library and not the aws-iot-device-sdk-js-v2 v2 library.

Could you tell me what error you have ? Also, I would also comment out the port and ALPNProtocols properties in the opts object to make sure that you are not having a different issue not linked to the ALPN options.

@RavirajKakadeGit
Copy link

yeah. I'm using v1 , after comment our port and ALPNProtocols its work but on mqtts protocol, I would like to enable socket on 443 with wss protocol

@HQarroum
Copy link
Author

HQarroum commented Feb 24, 2020

ALPN is a feature only implemented onmqtts, since wss already uses port 443, you must not specify ALPNProtocols in the options when you connect (see the docs on available protocols and ports). The above example demonstrates how to connect to AWS IoT through mqtts using ALPN extensions.

I made an edit to the Gist description to make it clearer.

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