Skip to content

Instantly share code, notes, and snippets.

@vuongngo
Created August 22, 2017 11:13
Show Gist options
  • Save vuongngo/16d38cc771a93053d907af9c952bb5df to your computer and use it in GitHub Desktop.
Save vuongngo/16d38cc771a93053d907af9c952bb5df to your computer and use it in GitHub Desktop.
import mqtt from 'mqtt';
export function mqclient(options) {
return new Promise((resolve, reject) => {
fetch('https://your-lambda-endpoint', {
method: 'get'
}).then((response) => {
const client = mqtt.connect(response.body.url, options);
resolve(client);
}).catch(err => reject(err));
});
}
mqclient()
.then(client => {
client.on('connect', () => {
// do something
// eg: client.publish('topic', 'message')
});
client.on('close', () => {
// do something
});
client.on('message', () => {
// do something
})
})
.catch(console.log);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment