Gregir
Gregir

Reputation: 1574

How to increase reliability of MQTT messages between AWS Lambda and web app via IoT

I'm having a problem with consistently receiving MQTT messages published by a Lambda function and subscribed to by a web application (using a "thing" in IoT called "workstation"). I sometimes get them right away but somewhat intermittently, but most often, I get them after a very long delay if I get them at all. I'm always able to connect and subscribe. I've tried qos 0 and 1.

EDIT: If I just use a generic topic string without the $aws/... prefix stuff, and qos of 1, I now tend to get a slim majority of the messages I send. But they are rarely immediate and they tend to stack up and come through several at a time. And several are lost altogether.

I have an Lambda function that serves an Alexa skill, and it publishes to an IoT MQTT topic/thing like this:

var params = {
  // have also tried topic strings without $aws/things/ prefix here
  topic: '$aws/things/workstation/', 
  payload: new Buffer('test message'),
  qos: 0
};

iotData.publish(params, function(err, data) {
  if (err) console.log('ERR: ', err); // an error occurred
  else if (data) console.log('DATA: ', data);  // successful response
});

I then have a web application using the AWS IOT Device SDK that subscribes to that MQTT channel and listens for messages like this:

var device = awsIoT.device({
    keyPath: './keys/abcd1234xyz-private.pem.key',
    certPath: './keys/abcd1234xyz-certificate.pem.crt',
    caPath: './keys/root-CA.crt',
    clientId: 'workstation',
    region: 'us-east-1',
    host: "abcd1234xyz.iot.us-east-1.amazonaws.com",
    port: 8883
});

device.on('connect', function() {
  console.log('connect');
  // have also tried topic strings without $aws/things/ prefix here
  device.subscribe('$aws/things/workstation/');
});

device.on('message', function(topic, payload) {
  console.log('message', topic, payload.toString());
});

Upvotes: 0

Views: 983

Answers (1)

Kyle Roche
Kyle Roche

Reputation: 426

Does every user of the web app end up with the same client ID? Client ID and certificate pair will disconnect any duplicate connections. Also, $aws is the prefix for shadow topics (and other diagnostic topics). Maybe use things/workstation?

Upvotes: 2

Related Questions