问题描述
请帮助,我需要在lambda函数中使用mqtt协议将一些数据发送给代理.我使用简单的代码对其进行测试:
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();
});
但是我收到错误消息找不到模块'mqtt'",如何在lambda函数中包含该模块?无论如何,我如何在lambda中使用mqtt?有人吗?
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???
推荐答案
首先,您将在项目目录中进行:
First you will do in the directory of your project:
npm install mqtt --save
将这个文件夹(在文件夹,文件和子目录内)压缩后,并上传到lambda函数.
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...
}
在AWS面板的lambda函数中,您将在Handler
文本字段中指定文件和正在使用的函数.
in your lambda function at the AWS panel you will appoint to the file and the function you are using in Handler
text field.
这篇关于适用于Alexa Javascript的AWS Lambda函数中的MQTT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!