问题
Please help, I need to use mqtt protocol in lambda function to send some data to a broker. I use simple code to test it :
mqtt = require('mqtt');
var client = mqtt.connect('mqtt://test.mosquitto.org');
client.on('connect', function () {
client.subscribe('presence');
client.publish('presence', 'Hello mqtt');
});
client.on('message', function (topic, message) {
// message is Buffer
console.log(message.toString());
client.end();
});
But I get an error "Cannot find module 'mqtt'", how can I include this module in the lambda function??? How can I use mqtt in my lambda anyways?? Somebody???
回答1:
First you will do in the directory of your project:
npm install mqtt --save
after you will zip this folder (inside the folder, the files and subdirectories) and upload to your lambda function.
Every time you must create a handler function, so you will create a function like this:
exports.handler = function (event, context, callback) {
... your code...
}
in your lambda function at the AWS panel you will appoint to the file and the function you are using in Handler
text field.
来源:https://stackoverflow.com/questions/38629993/mqtt-in-aws-lambda-function-for-alexa-javascript