AWS IoT - Two devices using the same clientId and certs cause re-connections error












1















I have a simple nodejs AWS Iot device implementation.



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

const device = awsIot.device({
keyPath: 'private.pem.key',
certPath: 'certificate.pem.crt',
caPath: 'CA.pem',
clientId: 'device1',
host: 'xxxxyyyyy1.iot.ap-southeast-1.amazonaws.com'
});

device.on('connect', () => {
console.log('DEVICE connect!');
});
device.on('message', (topic, payload) => {
console.log('DEVICE message!');
});
device.on('close', () => {
console.log('DEVICE closed!');
});
device.on('error', error => {
console.log('DEVICE error!');
console.log(error);
});
device.on('offline', () => {
console.log('DEVICE offline!');
});


The script runs fine if I execute a single instance of it.
Output:



dev@dev1:~/dev$ node test1.js
DEVICE connect!


However, if I run the same script on two different machines simultaneously, both devices repeatedly disconnects and reconnects.



dev@dev1:~/dev$ node test1.js
DEVICE connect!
DEVICE offline!
DEVICE closed!
DEVICE connect!
DEVICE offline!
DEVICE closed!
DEVICE connect!
DEVICE offline!
DEVICE closed!
DEVICE connect!
^C
dev@dev1:~/dev$


dev@dev2:~/dev$ node test1.js
DEVICE connect!
DEVICE offline!
DEVICE closed!
DEVICE connect!
DEVICE offline!
DEVICE closed!
DEVICE connect!
DEVICE offline!
DEVICE closed!
DEVICE connect!
^C
dev@dev2:~/dev2$


AWS Security Policy:



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









share|improve this question



























    1















    I have a simple nodejs AWS Iot device implementation.



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

    const device = awsIot.device({
    keyPath: 'private.pem.key',
    certPath: 'certificate.pem.crt',
    caPath: 'CA.pem',
    clientId: 'device1',
    host: 'xxxxyyyyy1.iot.ap-southeast-1.amazonaws.com'
    });

    device.on('connect', () => {
    console.log('DEVICE connect!');
    });
    device.on('message', (topic, payload) => {
    console.log('DEVICE message!');
    });
    device.on('close', () => {
    console.log('DEVICE closed!');
    });
    device.on('error', error => {
    console.log('DEVICE error!');
    console.log(error);
    });
    device.on('offline', () => {
    console.log('DEVICE offline!');
    });


    The script runs fine if I execute a single instance of it.
    Output:



    dev@dev1:~/dev$ node test1.js
    DEVICE connect!


    However, if I run the same script on two different machines simultaneously, both devices repeatedly disconnects and reconnects.



    dev@dev1:~/dev$ node test1.js
    DEVICE connect!
    DEVICE offline!
    DEVICE closed!
    DEVICE connect!
    DEVICE offline!
    DEVICE closed!
    DEVICE connect!
    DEVICE offline!
    DEVICE closed!
    DEVICE connect!
    ^C
    dev@dev1:~/dev$


    dev@dev2:~/dev$ node test1.js
    DEVICE connect!
    DEVICE offline!
    DEVICE closed!
    DEVICE connect!
    DEVICE offline!
    DEVICE closed!
    DEVICE connect!
    DEVICE offline!
    DEVICE closed!
    DEVICE connect!
    ^C
    dev@dev2:~/dev2$


    AWS Security Policy:



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









    share|improve this question

























      1












      1








      1








      I have a simple nodejs AWS Iot device implementation.



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

      const device = awsIot.device({
      keyPath: 'private.pem.key',
      certPath: 'certificate.pem.crt',
      caPath: 'CA.pem',
      clientId: 'device1',
      host: 'xxxxyyyyy1.iot.ap-southeast-1.amazonaws.com'
      });

      device.on('connect', () => {
      console.log('DEVICE connect!');
      });
      device.on('message', (topic, payload) => {
      console.log('DEVICE message!');
      });
      device.on('close', () => {
      console.log('DEVICE closed!');
      });
      device.on('error', error => {
      console.log('DEVICE error!');
      console.log(error);
      });
      device.on('offline', () => {
      console.log('DEVICE offline!');
      });


      The script runs fine if I execute a single instance of it.
      Output:



      dev@dev1:~/dev$ node test1.js
      DEVICE connect!


      However, if I run the same script on two different machines simultaneously, both devices repeatedly disconnects and reconnects.



      dev@dev1:~/dev$ node test1.js
      DEVICE connect!
      DEVICE offline!
      DEVICE closed!
      DEVICE connect!
      DEVICE offline!
      DEVICE closed!
      DEVICE connect!
      DEVICE offline!
      DEVICE closed!
      DEVICE connect!
      ^C
      dev@dev1:~/dev$


      dev@dev2:~/dev$ node test1.js
      DEVICE connect!
      DEVICE offline!
      DEVICE closed!
      DEVICE connect!
      DEVICE offline!
      DEVICE closed!
      DEVICE connect!
      DEVICE offline!
      DEVICE closed!
      DEVICE connect!
      ^C
      dev@dev2:~/dev2$


      AWS Security Policy:



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









      share|improve this question














      I have a simple nodejs AWS Iot device implementation.



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

      const device = awsIot.device({
      keyPath: 'private.pem.key',
      certPath: 'certificate.pem.crt',
      caPath: 'CA.pem',
      clientId: 'device1',
      host: 'xxxxyyyyy1.iot.ap-southeast-1.amazonaws.com'
      });

      device.on('connect', () => {
      console.log('DEVICE connect!');
      });
      device.on('message', (topic, payload) => {
      console.log('DEVICE message!');
      });
      device.on('close', () => {
      console.log('DEVICE closed!');
      });
      device.on('error', error => {
      console.log('DEVICE error!');
      console.log(error);
      });
      device.on('offline', () => {
      console.log('DEVICE offline!');
      });


      The script runs fine if I execute a single instance of it.
      Output:



      dev@dev1:~/dev$ node test1.js
      DEVICE connect!


      However, if I run the same script on two different machines simultaneously, both devices repeatedly disconnects and reconnects.



      dev@dev1:~/dev$ node test1.js
      DEVICE connect!
      DEVICE offline!
      DEVICE closed!
      DEVICE connect!
      DEVICE offline!
      DEVICE closed!
      DEVICE connect!
      DEVICE offline!
      DEVICE closed!
      DEVICE connect!
      ^C
      dev@dev1:~/dev$


      dev@dev2:~/dev$ node test1.js
      DEVICE connect!
      DEVICE offline!
      DEVICE closed!
      DEVICE connect!
      DEVICE offline!
      DEVICE closed!
      DEVICE connect!
      DEVICE offline!
      DEVICE closed!
      DEVICE connect!
      ^C
      dev@dev2:~/dev2$


      AWS Security Policy:



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






      javascript node.js aws-iot






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 16 '18 at 11:24









      user2176499user2176499

      488




      488
























          2 Answers
          2






          active

          oldest

          votes


















          1














          From AWS-IoT docs




          The message broker uses the client ID to identify each client. The client ID is passed in from the client to the message broker as part of the MQTT payload. Two clients with the same client ID are not allowed to be connected concurrently to the message broker. When a client connects to the message broker using a client ID that another client is using, a CONNACK message will be sent to both clients and the currently connected client will be disconnected.




          https://docs.aws.amazon.com/iot/latest/developerguide/protocols.html






          share|improve this answer































            1














            Well in MQTT, the protocol AWS IoT is using, the Clientid should be unique per Device




            The client identifier (ClientId) identifies each MQTT client that
            connects to an MQTT broker. The broker uses the ClientID to identify
            the client and the current state of the client.Therefore, this ID
            should be unique per client and broker. In MQTT 3.1.1 (the current
            standard), you can send an empty ClientId, if you don’t need a state
            to be held by the broker. The empty ClientID results in a connection
            without any state. In this case, the clean session flag must be set to
            true or the broker will reject the connection.




            The Client ID is also used for Client Takeover which causes your problem




            Usually, a disconnected client tries to reconnect. Sometimes, the
            broker still has an half-open connection for the client. In MQTT, if
            the broker detects a half-open connection, it performs a ‘client
            take-over’. The broker closes the previous connection to the same
            client (determined by the client identifier), and establishes a new
            connection with the client. This behavior ensures that the half-open
            connection does not stop the disconnected client from re-establishing
            a connection.




            https://www.hivemq.com/blog/mqtt-essentials/






            share|improve this answer
























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53336908%2faws-iot-two-devices-using-the-same-clientid-and-certs-cause-re-connections-err%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              From AWS-IoT docs




              The message broker uses the client ID to identify each client. The client ID is passed in from the client to the message broker as part of the MQTT payload. Two clients with the same client ID are not allowed to be connected concurrently to the message broker. When a client connects to the message broker using a client ID that another client is using, a CONNACK message will be sent to both clients and the currently connected client will be disconnected.




              https://docs.aws.amazon.com/iot/latest/developerguide/protocols.html






              share|improve this answer




























                1














                From AWS-IoT docs




                The message broker uses the client ID to identify each client. The client ID is passed in from the client to the message broker as part of the MQTT payload. Two clients with the same client ID are not allowed to be connected concurrently to the message broker. When a client connects to the message broker using a client ID that another client is using, a CONNACK message will be sent to both clients and the currently connected client will be disconnected.




                https://docs.aws.amazon.com/iot/latest/developerguide/protocols.html






                share|improve this answer


























                  1












                  1








                  1







                  From AWS-IoT docs




                  The message broker uses the client ID to identify each client. The client ID is passed in from the client to the message broker as part of the MQTT payload. Two clients with the same client ID are not allowed to be connected concurrently to the message broker. When a client connects to the message broker using a client ID that another client is using, a CONNACK message will be sent to both clients and the currently connected client will be disconnected.




                  https://docs.aws.amazon.com/iot/latest/developerguide/protocols.html






                  share|improve this answer













                  From AWS-IoT docs




                  The message broker uses the client ID to identify each client. The client ID is passed in from the client to the message broker as part of the MQTT payload. Two clients with the same client ID are not allowed to be connected concurrently to the message broker. When a client connects to the message broker using a client ID that another client is using, a CONNACK message will be sent to both clients and the currently connected client will be disconnected.




                  https://docs.aws.amazon.com/iot/latest/developerguide/protocols.html







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 16 '18 at 11:32









                  RoshanRoshan

                  1,7991921




                  1,7991921

























                      1














                      Well in MQTT, the protocol AWS IoT is using, the Clientid should be unique per Device




                      The client identifier (ClientId) identifies each MQTT client that
                      connects to an MQTT broker. The broker uses the ClientID to identify
                      the client and the current state of the client.Therefore, this ID
                      should be unique per client and broker. In MQTT 3.1.1 (the current
                      standard), you can send an empty ClientId, if you don’t need a state
                      to be held by the broker. The empty ClientID results in a connection
                      without any state. In this case, the clean session flag must be set to
                      true or the broker will reject the connection.




                      The Client ID is also used for Client Takeover which causes your problem




                      Usually, a disconnected client tries to reconnect. Sometimes, the
                      broker still has an half-open connection for the client. In MQTT, if
                      the broker detects a half-open connection, it performs a ‘client
                      take-over’. The broker closes the previous connection to the same
                      client (determined by the client identifier), and establishes a new
                      connection with the client. This behavior ensures that the half-open
                      connection does not stop the disconnected client from re-establishing
                      a connection.




                      https://www.hivemq.com/blog/mqtt-essentials/






                      share|improve this answer




























                        1














                        Well in MQTT, the protocol AWS IoT is using, the Clientid should be unique per Device




                        The client identifier (ClientId) identifies each MQTT client that
                        connects to an MQTT broker. The broker uses the ClientID to identify
                        the client and the current state of the client.Therefore, this ID
                        should be unique per client and broker. In MQTT 3.1.1 (the current
                        standard), you can send an empty ClientId, if you don’t need a state
                        to be held by the broker. The empty ClientID results in a connection
                        without any state. In this case, the clean session flag must be set to
                        true or the broker will reject the connection.




                        The Client ID is also used for Client Takeover which causes your problem




                        Usually, a disconnected client tries to reconnect. Sometimes, the
                        broker still has an half-open connection for the client. In MQTT, if
                        the broker detects a half-open connection, it performs a ‘client
                        take-over’. The broker closes the previous connection to the same
                        client (determined by the client identifier), and establishes a new
                        connection with the client. This behavior ensures that the half-open
                        connection does not stop the disconnected client from re-establishing
                        a connection.




                        https://www.hivemq.com/blog/mqtt-essentials/






                        share|improve this answer


























                          1












                          1








                          1







                          Well in MQTT, the protocol AWS IoT is using, the Clientid should be unique per Device




                          The client identifier (ClientId) identifies each MQTT client that
                          connects to an MQTT broker. The broker uses the ClientID to identify
                          the client and the current state of the client.Therefore, this ID
                          should be unique per client and broker. In MQTT 3.1.1 (the current
                          standard), you can send an empty ClientId, if you don’t need a state
                          to be held by the broker. The empty ClientID results in a connection
                          without any state. In this case, the clean session flag must be set to
                          true or the broker will reject the connection.




                          The Client ID is also used for Client Takeover which causes your problem




                          Usually, a disconnected client tries to reconnect. Sometimes, the
                          broker still has an half-open connection for the client. In MQTT, if
                          the broker detects a half-open connection, it performs a ‘client
                          take-over’. The broker closes the previous connection to the same
                          client (determined by the client identifier), and establishes a new
                          connection with the client. This behavior ensures that the half-open
                          connection does not stop the disconnected client from re-establishing
                          a connection.




                          https://www.hivemq.com/blog/mqtt-essentials/






                          share|improve this answer













                          Well in MQTT, the protocol AWS IoT is using, the Clientid should be unique per Device




                          The client identifier (ClientId) identifies each MQTT client that
                          connects to an MQTT broker. The broker uses the ClientID to identify
                          the client and the current state of the client.Therefore, this ID
                          should be unique per client and broker. In MQTT 3.1.1 (the current
                          standard), you can send an empty ClientId, if you don’t need a state
                          to be held by the broker. The empty ClientID results in a connection
                          without any state. In this case, the clean session flag must be set to
                          true or the broker will reject the connection.




                          The Client ID is also used for Client Takeover which causes your problem




                          Usually, a disconnected client tries to reconnect. Sometimes, the
                          broker still has an half-open connection for the client. In MQTT, if
                          the broker detects a half-open connection, it performs a ‘client
                          take-over’. The broker closes the previous connection to the same
                          client (determined by the client identifier), and establishes a new
                          connection with the client. This behavior ensures that the half-open
                          connection does not stop the disconnected client from re-establishing
                          a connection.




                          https://www.hivemq.com/blog/mqtt-essentials/







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 16 '18 at 11:30









                          wiomocwiomoc

                          487512




                          487512






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53336908%2faws-iot-two-devices-using-the-same-clientid-and-certs-cause-re-connections-err%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Bressuire

                              Vorschmack

                              Quarantine