Recently, I have been using the MQTT protocol in the development process of IoT. During the development process, I encountered a problem: every time a connection is established, it quickly disconnects and reconnects, and then continuously disconnects and reconnects
Firstly, the subscription end (device end) is completely separate from the push end (publish)
The identities of the former and the latter can be exchanged at any time, and they both maintain separate communication with the MQTT server. This way, when establishing a connection between the subscription and push ends, it is important to note that the connection is not for obtaining subscriptions or sending push information
The clientID should be independent and unique, for example, using a timestamp+uid method. When publishing a push, the device ID should be combined and sent. When subscribing, the device ID can be identified to receive push messages
The main mistake I made was that when establishing a connection between the subscription and push ends, I only used the form of topic and device ID to build the clientID. This resulted in the clientIDs on both ends being completely consistent, leading to the problem of client conflict
The solution is simply to add a timestamp and uid to the client