MQTT clientId should be unique

Each mqtt hook establishes separate connection to the MQTT broker. If
their clientIds are all equal the MQTT broker will disconnect the clients - the
protocol does not allow 2 connected clients with the same name
This commit is contained in:
Plamen Todorov 2019-10-06 22:08:43 +03:00
parent 5abadd72a3
commit fb2aef2ce6
1 changed files with 3 additions and 1 deletions

View File

@ -83,7 +83,9 @@ func (conn *MQTTConn) Send(msg string) error {
}
ops = ops.SetTLSConfig(&config)
}
ops = ops.SetClientID("tile38").AddBroker(uri)
nano := time.Now().UnixNano()
clientID := fmt.Sprintf("tile38_%x", nano) //the id of connected clients should be unique
ops = ops.SetClientID(clientID).AddBroker(uri)
c := paho.NewClient(ops)
if token := c.Connect(); token.Wait() && token.Error() != nil {