MQTT in AWS Lambda function for Alexa Javascript

天涯浪子 提交于 2019-12-13 07:17:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!