Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MassimoSporchia/1b4c0435e7b0d3a539098c01403aca6a to your computer and use it in GitHub Desktop.
Save MassimoSporchia/1b4c0435e7b0d3a539098c01403aca6a 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));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment