Skip to content

Instantly share code, notes, and snippets.

@Pwntus
Created April 24, 2018 17:06
Show Gist options
  • Save Pwntus/7e2d5a79071fd9c9b398680cef6008b5 to your computer and use it in GitHub Desktop.
Save Pwntus/7e2d5a79071fd9c9b398680cef6008b5 to your computer and use it in GitHub Desktop.
Initializing MQTT client with Cognito
var MIC = require('mic-sdk-js').default;
var AWSMqtt = require('aws-mqtt-client');
var api = new MIC;
// Init by providing the endpoint for your app
api.init('startiot.mic.telenorconnexion.com')
.then((manifest, credentials) => {
// Login a user
api.login('USERNAME', '*********')
.then(user => {
// Init a new MQTT client
var mqttClient = new AWSMqtt({
region: api._AWS.config.region,
accessKeyId: api._AWS.config.credentials.accessKeyId,
secretAccessKey: api._AWS.config.credentials.secretAccessKey,
sessionToken: api._AWS.config.credentials.sessionToken,
endpointAddress: api._manifest.IotEndpoint,
maximumReconnectTimeMs: 8000,
protocol: 'wss'
})
//mqttClient.on('reconnect', () => reconnect())
mqttClient.on('connect', () => connect())
//mqttClient.on('close', () => close())
//mqttClient.on('error', (e) => error(e))
mqttClient.on('message', (topic, message) => message(topic, message))
var connect = function () {
mqttClient.subscribe('thing-update/my/topic/#', {qos: 1}, (err, granted) => {
if (err)
console.log(err)
})
}
var message = function (topic, message) {
console.log(topic, message.toString('utf-8'))
}
});
})
.catch(err => console.log('Error: ', err));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment