I am a bit confused about using random client ID as shown in : Getting Started with MQTT on Arduino Using NodeMCU ESP8266 - by Kudzai Manditereza
FEB 3, 2023
/************* Connect to MQTT Broker ***********/
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
String clientId = "ESP8266Client-"; // Create a random client ID
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str(), mqtt_username, mqtt_password)) {
Serial.println("connected");
client.subscribe("led_state"); // subscribe the topics here
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds"); // Wait 5 seconds before retrying
delay(5000);
}
}
}
Do HIVEMQ need random client ID when connecting to the MQTT Broker? AS far I know, the client ID is unique and constant.
Thank you for reaching out to us with your query about MQTT client IDs. We appreciate your interest in optimizing your connection to our MQTT broker.
Using a random client ID is not a requirement when connecting to the MQTT broker. The choice of client ID is entirely up to you, with the only crucial factor being that each client ID must be unique among all clients connected to the broker.
If you prefer, you can certainly use a constant client ID. The key is to ensure it remains unique for each client. This approach can work well, especially if you have a system for managing and tracking your client IDs.
For more detailed information on this topic, you might find our blog post on client takeover in MQTT helpful. It discusses the importance of unique client IDs and their role in the MQTT protocol.
Please don’t hesitate to reach out if you have any further questions or need additional clarification.