Connecting several devices with same credentials (how to avoid MQTT connection replace, connection replacement)


This is very good question because it touches a particular MQTT behavior related to connection replace when connecting for second time using same credentials.

This is useful to protect devices from been blocked by stalled connections due to NAT problems.

However, as you have noticed, it creates problems in environments where the same clientId+userName is used to connect (and subscribe) and to PUBLISH.

It also creates problems with integrations where same user/credentials are used to publish several messages at the same time in a concurrent manner. For example, a web page triggering PUBLISH by every visit. It is likely several visits can happen at the same time.

As you can see it has pros and cons. Now, to solve the issue you can:

  1. Use different users to publish and subscribe, paying attention not to reuse them (only one time per credential).

  2. Or, disable “Connection replace” so you can connect several times without closing previous connection.

If you opt for second option, here is how. Go and click on the device where you want to enable connection replace:

After clicking on “conn replace off”, you will be able to connect several times without receiving a connection replace.

Some notes about disabling connection replace

  1. It is not standard :slight_smile:

  2. Every device connected with these credentials will receive a virtual temporal clientId with cleanSession enforced to true (subscriptions will be lost for example, so you have to resend them).

Publish without Device Registration?
Publish without Device Registration?