question

WhatIsMyRealName avatar image
WhatIsMyRealName asked

Can't call out to MQTT from Lambda in Alexa context

Hi I've been able to connect (publish) to MQTT from Lambda as described in this article: http://2lemetry.com/2014/12/05/native-mqtt-lambda/ But when I try to do the same in the context of an Alexa request I can't make it work. For example, take the very simple HelloWorld from AlexaLambdaExamples, and add in this block of code in an IntentHandler (don't forget to put 'var mqtt = require('mqtt');' at the top of the page): var client = mqtt.connect("mqtt:// : @ :1883"); client.publish("my/mqtt/topic", "Hello world from simple alexa lambda app", false); client.end(); client.on("close", (function () { return context.done(null, "DONE"); })); client.on("error", (function () { return context.done(null, "ERROR"); })); I've been beating my head against this for 10 straight hours. Any ideas?? Thanks!!
alexa skills kitsubmission testing certification
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

WhatIsMyRealName avatar image
WhatIsMyRealName answered
> Hi > > I've been able to connect (publish) to MQTT from > Lambda as described in this article: > > http://2lemetry.com/2014/12/05/native-mqtt-lambda/ > > But when I try to do the same in the context of an > Alexa request I can't make it work. For example, > take the very simple HelloWorld from > AlexaLambdaExamples, and add in this block of code in > an IntentHandler (don't forget to put 'var mqtt = > require('mqtt');' at the top of the page): > > var client = > mqtt.connect("mqtt:// : @ :1883") > > client.publish("my/mqtt/topic", "Hello world > from simple alexa lambda app", false); > client.end(); > client.on("close", (function () { > // PUT CALLBACK HERE in my case response.tellWithCard(); > return context.done(null, "DONE"); > )); > > client.on("error", (function () { > return context.done(null, "ERROR"); > })); > > been beating my head against this for 10 straight > hours. Any ideas?? > > Thanks!! SOLVED I suspected that it might be a problem with asynchronous callbacks and the timing of context.done or context.success, and this appears to be the case: https://aws.amazon.com/blogs/compute/container-reuse-in-lambda/ I solved this by putting the callback inside the MQTT Client OnClose handler, so the context only gets closed after MQTT has returned. Anyone that's not getting HTTP responses from Lambda, or any other callout, good money says this is your issue.
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.