@@ -67,7 +67,6 @@ func localConfigs() {
6767 }
6868 hostIP := string (utils .TrimSpace (data ))
6969 pubsubHost = utils .Sprintf ("%s:%s" , hostIP , pubsubPort )
70-
7170 if err := os .Setenv ("PUBSUB_EMULATOR_HOST" , pubsubHost ); err != nil {
7271 utils .Log (utils .LOG_ERROR , utils .Sprintf ("failed to set emulator HOST: %v" , err ))
7372 }
@@ -123,7 +122,10 @@ func ConnectMQTT() *mqtt.ClientOptions {
123122
124123func SubscribeMQTT (client mqtt.Client ) {
125124 token := client .Subscribe (MqttTopicPath , QOS , mqttMessageHandler )
126- token .Wait ()
125+ if token .Wait () && token .Error () != nil {
126+ utils .Log (utils .LOG_ERROR , utils .Sprintf ("MQTT subscribe failed: %v" , token .Error ()))
127+ panic (token .Error ())
128+ }
127129 utils .Log (utils .LOG_DEBUG , utils .Sprintf ("subscribed to MQTT topic: %s" , MqttTopicPath ))
128130}
129131
@@ -176,6 +178,21 @@ func confPubSub(projectID string) (context.Context, *pubsub.Client) {
176178}
177179
178180func publishTopic (msg []byte ) {
179- publisher .Publish (pubctx , & pubsub.Message {Data : msg })
180- utils .Log (utils .LOG_INFO , utils .Sprintf ("queued message for publishing to pubsub: %s" , msg ))
181+ payload := append ([]byte (nil ), msg ... )
182+ utils .Log (utils .LOG_INFO , utils .Sprintf ("queued message for publishing to pubsub.... %s" , string (payload )))
183+ utils .Log (utils .LOG_DEBUG , utils .Sprintf ("queued message bytes: %v" , payload ))
184+
185+ // publish and ack
186+ result := publisher .Publish (pubctx , & pubsub.Message {Data : payload })
187+ id , err := result .Get (pubctx )
188+ if err != nil {
189+ utils .Log (utils .LOG_ERROR , utils .Sprintf ("failed to publish to pubsub: %v" , err ))
190+ return
191+ }
192+ utils .Log (utils .LOG_INFO , utils .Sprintf ("message published to pubsub with id: %s" , id ))
181193}
194+
195+ // func publishTopic(msg []byte) {
196+ // publisher.Publish(pubctx, &pubsub.Message{Data: msg})
197+ // utils.Log(utils.LOG_INFO, utils.Sprintf("queued message for publishing to pubsub: %s", msg))
198+ // }
0 commit comments