MQTT disconnects constantly
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
asked
8 years ago
2.3K views
lg
...
6 Answers
-
Newest
-
Most votes
-
Most comments
Are these answers helpful? Upvote the correct answer to help the community benefit from your knowledge.
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
8 years ago
lg
...
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.
answered
8 years ago
lg
...
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
7 years ago
lg
...
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.
:-(
answered
4 years ago
lg
...
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
4 years ago
lg
...
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.
Farid
6 months ago
I had the same issue and spent hours trying to figure it out. Then I read on github that it was due to missing permission. The error code generated is confusing but if you check the logs you'll see that some permission were missing.
https://github.com/aws/aws-iot-device-sdk-python-v2/discussions/329