connection lost - will attempt reconnection in 1 seconds...
See original GitHub issueHi,
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:
- Created 7 years ago
- Comments:31 (1 by maintainers)

Top Related StackOverflow Question
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
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: