MQTT disconnects constantly

0

Hi,
I wanted to push Updates over AWS IOT to a device. The Structure is as follows:
API Gateway -> Lambda -> Update Device Shadow -> Client Device gets notified over MQTT (Nodejs SDK)

This works fine in principal and it only needs some small changes from the example code. However the MQTT Connection gets disrupted constantly which results in "connected" being printed (see code) about every 5-20 seconds. Sometimes it can stay connected for some minutes but this is rare.
I rules out networking issues as I tested it on different machines in different networks (also in a VM in my university network).

I think my code is fine:

var awsIot = require('aws-iot-device-sdk');

var device = awsIot.device({
    keyPath:  "mySuperSecret.private.key",
    certPath: "mySuperSecret.cert.pem",
    caPath: "root-CA.crt",
    clientId: "clientIdDefault",
    region: "eu-west-1"
});

device
    .on('connect', function() {
        console.log('MQTT connected @ '+ Math.floor(Date.now() / 1000));
        device.subscribe('text_topic');
    });

device.on('offline', function () {
    console.log("MQTT offline");
});

device.on('reconnect', function () {
    console.log("MQTT reconnecting");
});

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

which produces the following output:

MQTT connected @ 1494497750
MQTT offline
MQTT reconnecting
MQTT connected @ 1494497753
MQTT offline
MQTT reconnecting
MQTT connected @ 1494497758
MQTT offline
MQTT reconnecting
MQTT connected @ 1494497766
MQTT offline
MQTT reconnecting
MQTT connected @ 1494497783
MQTT offline
MQTT reconnecting
MQTT connected @ 1494497816
MQTT offline
MQTT reconnecting
MQTT connected @ 1494497850
MQTT offline
MQTT reconnecting
MQTT connected @ 1494497916

Any ideas on the issue and what might cause this?

Best regards,
Daniel

daniel7
asked 7 years ago1550 views
6 Answers
0

Hi,

What SDK did you use in your lambda function? Are you using the same client Id as you used in device? The client id should be UUID for each connection.

Thanks,
Fengyi

answered 7 years ago
0

I did not use any sdk in my lambda function. I update the device over the HTTP endpoint. The client ids should be ok.

For now I cannot reproduce the misbehaviour anymore. It's working fine now and I don't know why because I haven't changed anything.

I'm marking this question as answered.

daniel7
answered 7 years ago
0

I want to reopen this issue. I am facing the same issue.

I am trying to do a very simple feature :
javascript(subscribe.js) -> aws iot (mqtt broker)

javascript(publish.js) -> aws iot (mqtt broker)

i am only trying to utilise teh mqtt broker capability of aws, that is , i am trying to use aws iot as an mqtt broker.
from the management console i am able to hold pub/sub.

PFB code for subscribe.js:

var awsIot = require('aws-iot-device-sdk');

//
// Replace the values of '<YourUniqueClientIdentifier>' and '<YourCustomEndpoint>'
// with a unique client identifier and custom host endpoint provided in AWS IoT.
// NOTE: client identifiers must be unique within your AWS account; if a client attempts 
// to connect with a client identifier which is already in use, the existing 
// connection will be terminated.
//
var device = awsIot.device({
    keyPath : 'secret.pem.key',
                certPath:'secret.pem.crt',
                caPath : 'rootCA.pem',
    clientId : 'id121',
    host : 'secret.iot.secret.amazonaws.com'
});

//
// Device is an instance returned by mqtt.Client(), see mqtt.js for full
// documentation.
//
device
  .on('connect', function() {
    console.log('connect');
    device.subscribe('topic_2/#');
    
  });

device
  .on('message', function(topic, payload) {
    console.log('message', topic, payload.toString());
  });
/*device
      .on('close', function() {
         console.log('close');
   });
   device
      .on('reconnect', function() {
         console.log('reconnect');
      });
   device
      .on('offline', function() {
         console.log('offline');
      });
   device
      .on('error', function(error) {
         console.log('error', error);
      });*/

PFB code for publish.js:

var awsIot = require('aws-iot-device-sdk');

//
// Replace the values of '<YourUniqueClientIdentifier>' and '<YourCustomEndpoint>'
// with a unique client identifier and custom host endpoint provided in AWS IoT.
// NOTE: client identifiers must be unique within your AWS account; if a client attempts 
// to connect with a client identifier which is already in use, the existing 
// connection will be terminated.
//
var device = awsIot.device({
    keyPath : 'secret.pem.key',
                certPath:'secret.pem.crt',
                caPath : 'rootCA.pem',
    clientId : 'id121',
    host : 'secret.iot.secret.amazonaws.com'
});

//
// Device is an instance returned by mqtt.Client(), see mqtt.js for full
// documentation.
//
device
  .on('connect', function() {
    console.log('connect');
    //device.subscribe('topic_2/#');
    device.publish('topic_2/2', 'hello');
  });
/*
device
  .on('message', function(topic, payload) {
    console.log('message', topic, payload.toString());
  });
device
      .on('close', function() {
         console.log('close');
   });
   device
      .on('reconnect', function() {
         console.log('reconnect');
      });
   device
      .on('offline', function() {
         console.log('offline');
      });
   device
      .on('error', function(error) {
         console.log('error', error);
      });*/

Please help !!

thanks
Aarushi

answered 6 years ago
0

I'm having the same issue with a similar code. To see that there was never a resolution posted for this is not encouraging.

:-(

j2inet
answered 3 years ago
0

The immediate problem is that both clients use the same client ID

clientId : 'id121',

Good luck.
If you want to learn AWS IoT in minutes, check out https://mqttlab.iotsim.io/aws

answered 3 years ago
0

To address the constant disconnections you are seeing with the MQTT client, try these things out:

  • Verify that the MQTT client is using the correct AWS IoT endpoint, certificate and key files. Even a small error could cause connectivity issues.
  • Check for any network firewalls or proxies that could be terminating idle connections. AWS IoT uses keepalive to maintain connections but intermediaries may still interfere.
  • Monitor the AWS IoT console and CloudWatch metrics for any spikes in errors or throttling that could indirectly impact clients.
  • Try reducing the MQTT QoS level, keepalive time or increasing timeout thresholds in the client to make it more robust to temporary disruptions.
  • As a test, try running the client directly on an EC2 instance within the same AWS region to bypass any external network variability.
  • Review client code for any exceptions or errors during connection or message handling. Debug logs may provide more insight into the disconnect reasons. Proper configuration of TLS, certificates and network accessibility are key for reliable MQTT connectivity.
profile picture
EXPERT
answered 10 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions