简体   繁体   English

适用于Alexa Javascript的AWS Lambda函数中的MQTT

[英]MQTT in AWS Lambda function for Alexa Javascript

Please help, I need to use mqtt protocol in lambda function to send some data to a broker. 请帮忙,我需要在lambda函数中使用mqtt协议将一些数据发送到代理。 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??? 但是我收到一个错误“找不到模块'mqtt'”,我如何在lambda函数中包含该模块? How can I use mqtt in my lambda anyways?? 无论如何,我如何在lambda中使用mqtt? Somebody??? 有人吗?

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. 在您压缩该文件夹(在文件夹,文件和子目录内)并上传到lambda函数之后。

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. 在AWS面板的lambda函数中,您将在Handler文本字段中指定文件和正在使用的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM