Skip to content

Instantly share code, notes, and snippets.

@Pwntus
Last active October 29, 2018 15:56
Show Gist options
  • Save Pwntus/5728e13e2c3f78550f997886e1a83e14 to your computer and use it in GitHub Desktop.
Save Pwntus/5728e13e2c3f78550f997886e1a83e14 to your computer and use it in GitHub Desktop.
MQTT client with Cogntio authentication and token refresh
let MIC = require('mic-sdk-js').default
let AWSMqtt = require('aws-mqtt-client').default
const CONFIG = {
// Username of the Cognito user
username: '',
// Password of the Cognito user
password: '',
// The application endpoint
app: 'startiot.mic.telenorconnexion.com',
// The MQTT topic, wildcards can be used (case sensitive)
topic: 'thing-update/My/Domain/#',
// MQTT reconnect counter
retries: 0
}
// Instantiate a new Managed IoT Cloud API object
let api = new MIC
// Init by providing the app endpoint
api.init(CONFIG.app)
// The manifest is fetched and a 'unauthorized'
// Cognito identity is created
.then((manifest, credentials) => {
// Login the Cognito user
return api.login(CONFIG.username, CONFIG.password)
.then(user => {
// Init MQTT client with Cognito credentials
let mqtt = new AWSMqtt({
region: manifest.Region,
accessKeyId: api._AWS.config.credentials.accessKeyId,
secretAccessKey: api._AWS.config.credentials.secretAccessKey,
sessionToken: api._AWS.config.credentials.sessionToken,
endpointAddress: manifest.IotEndpoint,
maximumReconnectTimeMs: 8000,
protocol: 'wss' // Websockets
})
mqtt.on('connect', () => {
console.log('Connecting to topic')
mqtt.subscribe(CONFIG.topic, {qos: 1}, (err, granted) => {
if (err)
console.log(err)
console.log('Connected!')
})
})
mqtt.on('message', (topic, message) => {
console.log('Message: ', topic, message.toString())
})
mqtt.on('reconnect', () => {
api._refreshCredentials(api.refreshToken).then(() => {
// Increment MQTT reconnect counter
CONFIG.retries++
console.log('Refreshed credentials')
// Give up after 3 failed attempts
if (CONFIG.retries >= 2) {
console.log('Maximum failed attempts reached, closing connection...')
CONFIG.retries = 0
mqtt.close(true) // 'true' signifes a forced close
}
})
.catch(err => console.error(err))
})
})
})
.catch(err => console.error(err))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment