From 6fab87e35cf0db1a05eb2fbd58442f989913058e Mon Sep 17 00:00:00 2001 From: Kristjan Komlosi Date: Sun, 28 Dec 2025 19:13:44 +0100 Subject: [PATCH] subscribe: set client_id for MQTT subscriber to ensure unique identification --- internal/handler/mqttping.go | 30 +++++++++++++++--------------- test/subscribe.py | 1 + 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/internal/handler/mqttping.go b/internal/handler/mqttping.go index 89f2b2b..4c317f6 100644 --- a/internal/handler/mqttping.go +++ b/internal/handler/mqttping.go @@ -1,24 +1,24 @@ package handler import ( - "net/http" - "os" - "time" + "net/http" + "os" + "time" - "github.com/gin-gonic/gin" - "git.piskot.si/SeminarM2/lambdaiot-core/internal/mqtt" + "git.piskot.si/SeminarM2/lambdaiot-core/internal/mqtt" + "github.com/gin-gonic/gin" ) // MQTTPing publishes the current timestamp to the configured MQTT topic func MQTTPing(c *gin.Context) { - ts := time.Now().Format(time.RFC3339) - topic := os.Getenv("MQTT_TOPIC") - if topic == "" { - topic = "lambdaiot" - } - if err := mqtt.PublishDefault(topic, []byte(ts)); err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) - return - } - c.JSON(http.StatusOK, gin.H{"timestamp": ts}) + ts := time.Now().Format(time.RFC3339) + topic := os.Getenv("MQTT_TOPIC") + if topic == "" { + topic = "lambdaiot" + } + if err := mqtt.PublishDefault(topic, []byte(ts)); err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } + c.JSON(http.StatusOK, gin.H{"timestamp": ts}) } diff --git a/test/subscribe.py b/test/subscribe.py index bd4c214..d23e5c0 100644 --- a/test/subscribe.py +++ b/test/subscribe.py @@ -32,6 +32,7 @@ client.on_connect = on_connect client.on_message = on_message if username: client.username_pw_set(username, password) + client.client_id = f"subscribe-{os.getpid()}" try: client.connect(host, port, 60)