Search code examples
amazon-web-servicesmqttpublish

Not able to publish data on aws mqtt broker, its getting stuck at publish after getting connected status 'true' in below code


I am trying to connect my NodeJS client to aws mqtt broker. In below client code i am getting 'connected true' in response but could not able to publish data.

var mqtt    = require('mqtt');

var KEY = fs.readFileSync('xx.key');
var CERT = fs.readFileSync('xx.pem.crt');

var options=
{
clientId:"js111",
rejectUnauthorized : false,
key: KEY,
cert: CERT,
}

var client  = mqtt.connect("mqtts://xx.amazonaws.com:8883",options);

client.on('error', function (err) {
    console.log(err);
  });

client.on("connect",function(){ 
console.log("connected  "+ client.connected);

    client.publish('test1', "This is test msg", {qos: 2}, (err) => {
                  if (err) console.log('error occur: ', err);
                  else console.log('message successfully publish');
        });

})

Solution

  • You are publishing your message with {qos: 2}; this is not supported by AWS IoT:

    AWS IoT and the AWS IoT Device SDKs support the MQTT Quality of Service (QoS) levels 0 and 1. The MQTT protocol defines a third level of QoS, level 2, but AWS IoT does not support it.

    Note: As you have not provided much detail in your question (error messages etc) this may not be the full issue (but attempting to publish a QOS2 message will result in your connection being dropped).