question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

connection lost - will attempt reconnection in 1 seconds...

See original GitHub issue

Hi,

I am trying to create a basic IoT appliance. I’m using a Raspberry Pi 3 with Raspbian, but I have also reproduced this error on Windows 10.

I have the following code:

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

var device = awsIot.device({
    keyPath: "/home/pi/share/MyDevice/private.pem.key",
    certPath: "/home/pi/share/MyDevice/certificate.pem.crt",
    caPath: "/home/pi/share/MyDevice/root-CA.pem",
    clientId: "MyDevice",
    host: "XXXXXXXXXXXXXX.iot.ap-southeast-1.amazonaws.com",
    region: "ap-southeast-1",
    port: 8883,
    debug: true
});

device.on('connect', function() {
    console.log('connected');
    device.subscribe('myevent');
    device.publish('deviceonline', JSON.stringify({ online: true}));
});

device.on('close', function() {
    console.log('disconnected', arguments);
});

device.on('error', function() {
    console.log('error', arguments);
});

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

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

device.on('timeout', function(thingName, clientToken) {
    console.log('received timeout');
});

When I run this I get the following output

attempting new mqtt connection… connection lost - will attempt reconnection in 1 seconds… disconnected {} reconnecting {} connection lost - will attempt reconnection in 2 seconds… disconnected {} reconnecting {} connection lost - will attempt reconnection in 4 seconds… disconnected {} reconnecting {} connection lost - will attempt reconnection in 8 seconds… disconnected {} reconnecting {} connection lost - will attempt reconnection in 16 seconds… disconnected {} reconnecting {} connection lost - will attempt reconnection in 32 seconds… disconnected {} reconnecting {} connection lost - will attempt reconnection in 64 seconds… disconnected {} reconnecting {} connection lost - will attempt reconnection in 128 seconds… disconnected {} reconnecting {} connection lost - will attempt reconnection in 128 seconds… disconnected {}

I’m able to connect by changing the protocol to wss, but I would much rather use MQTT.

Thanks,

YM

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:31 (1 by maintainers)

github_iconTop GitHub Comments

9reactions
rongsawscommented, May 3, 2016

Could you please double check if your certificate (/home/pi/share/MyDevice/certificate.pem.crt) is attached to a policy that allows the client to connect to IoT? If you connect your device through the AWS IoT Console (Thing Page -> Connect a device -> Node JS -> Generate certificate and policy), a default policy, named <ThingName>-Policy, should be created and attached to the certificate with content like the below. You could always attach your own policy to the certificate through the console or command line, see here for more details http://docs.aws.amazon.com/cli/latest/reference/iot/index.html and http://docs.aws.amazon.com/iot/latest/developerguide/authorization.html?shortFooter=true.

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Action": [
        "iot:*"
      ],
      "Resource": [
        "*"
      ],
      "Effect": "Allow"
    }
  ]
}

-Rong

3reactions
rongsawscommented, Aug 16, 2016

Can you check if the certificate you are connecting with is activated and attached with a policy that allows you to connect? You could do that on the AWS IoT console or use AWS cli like so:

$ aws iot describe-certificate --certificate-id  <cert-id>

$ aws iot list-principal-policies --principal <cert-arn>

$ aws iot get-policy --policy-name <policy-name>
Read more comments on GitHub >

github_iconTop Results From Across the Web

React & AWS IoT MQTT over WSS - "connection lost
js:781 connection lost - will attempt reconnection in 128 seconds... JS Code To Create the Device and Connect: var device = awsIot.device({ ...
Read more >
MQTT reconnection problem - FreeRTOS Community Forums
i set keep alive time to 30 seconds, now controller has connected to mqtt and subscribed to one topic. suppose it lost the...
Read more >
MqttConnectionConfig | aws-iot-device-sdk-v2
The server will assume the connection is lost if no PING is received after 1.5X this value. ... Maximum seconds to wait between...
Read more >
Paho Asynchronous MQTT C Client Library - Eclipse
The minimum and maximum times before the next connection attempt can also be set, the defaults being 1 and 60 seconds. At each...
Read more >
ASP.NET Core SignalR JavaScript client - Microsoft Learn
Install the SignalR client package; Connect to a hub ... If the first reconnect attempt fails, the second reconnect attempt also starts ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found