0

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???

hardillb
  • 54,545
  • 11
  • 67
  • 105
Suki
  • 27
  • 6
  • We need some more context here, are we talking nodejs? If so have you installed the mqtt npm module? – hardillb Jul 28 '16 at 08:26
  • Yes, node.js and I've installed mqtt npm. – Suki Jul 28 '16 at 08:44
  • Update the question with details of how and where you npm installed the mqtt module and exactly how/where you are executing the code listed above. The error implies that the mqtt module is not installed where it needs to be – hardillb Jul 28 '16 at 09:11
  • Trying to execute the code in th AWS Labda. I have my Lambda function and an mqtt module which I've included in the Lambda function. I installed it on Ubuntu, not sure what you mean 'how'. – Suki Jul 28 '16 at 09:42

1 Answers1

3

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.

pedro.olimpio
  • 1,478
  • 2
  • 22
  • 43
  • 1
    @Suki You're welcome, don't forget of upvote the answer. – pedro.olimpio May 25 '17 at 13:30
  • @Suki how did you make it work? I have tried with lambda but it didn't work. Basically, it didn't give any output. ```"use strict"; const mqtt = require('mqtt'); const client = mqtt.connect('mqtt://test.mosquitto.org'); module.exports.startPolling = async (event, context) => { client.on('connect', () => { client.subscribe('presence'); client.publish('presence', 'Hello mqtt'); }); client.on('message', (topic, message) => { // message is Buffer console.log(message.toString()); client.end(); }); }; ``` – Chandara Chea Nov 07 '19 at 20:43
  • @ChandaraChea At the console of `Lambda` are there no messages? Are there another plase you have `context.succeed()` or another behavior? This code above is the entire code? – pedro.olimpio Nov 08 '19 at 18:03
  • 1
    @pedro.olimpio it didn't behave correctly. For example, after triggering lambda for many times then it said memory leaking. Anyway, I managed to solve it by using async-mqtt. Thank you for answering :) – Chandara Chea Nov 08 '19 at 18:23
  • @pedro.olimpio I think this weird behavior is from using callback function in Lambda, which is really strange. I have tried with ```setTimeout(function () { console.log('Timeout complete.') }, 5000); ``` and it showed the same behavior. It sometimes didn't show anything or sometimes showed more than once. And the context.succeed() is undefined. Do you happen to know why is it like this? – Chandara Chea Nov 08 '19 at 18:51