Created
August 22, 2017 11:13
-
-
Save vuongngo/16d38cc771a93053d907af9c952bb5df to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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