aboutsummaryrefslogtreecommitdiff
path: root/catbus.go
diff options
context:
space:
mode:
Diffstat (limited to 'catbus.go')
-rw-r--r--catbus.go11
1 files changed, 11 insertions, 0 deletions
diff --git a/catbus.go b/catbus.go
index 68dee1b..2a3a120 100644
--- a/catbus.go
+++ b/catbus.go
@@ -5,7 +5,10 @@
package catbus
import (
+ "fmt"
"math/rand"
+ "os"
+ "path"
"sync"
"time"
@@ -73,6 +76,7 @@ func NewClient(brokerURI string, options ClientOptions) Client {
mqttOpts := mqtt.NewClientOptions()
mqttOpts.AddBroker(brokerURI)
mqttOpts.SetAutoReconnect(true)
+ mqttOpts.SetClientID(defaultClientID())
mqttOpts.SetOnConnectHandler(func(_ mqtt.Client) {
client.stopAllTimers()
client.startAllTimers()
@@ -185,3 +189,10 @@ func messageFromMQTTMessage(msg mqtt.Message) Message {
Topic: msg.Topic(),
}
}
+
+func defaultClientID() string {
+ binary := path.Base(os.Args[0])
+ hostname, _ := os.Hostname()
+ pid := os.Getpid()
+ return fmt.Sprintf("%s_%s_%d", binary, hostname, pid)
+}