MQTT Issues

have some questions about MQTT Library

if i do Subscribe for an message (and if on Subscribe TOPICS values (PAYLOADS) are changed on BROKER)
did this should need to be appeared on on

Subscribe_Changed …
private static void Client_SubscribedChanged(object sender, MqttPacket packet)

or in PublishReceived …
private static void Client_PublishReceivedChanged(object sender, MqttPacket packet)

I believe
Subscribe_Changed raise event after Subscribe successful.

PublishReceived raise event after got msg from broker.

1 Like

how to know from which subscribe came which payload ??

supposed to i have

            string subcribe1= "home/temperature";
            string subcribe2= "home/humidity";

// Subscribe to a topic before start with publish ...
            client.Subscribe(new string[] { subcribe1, subcribe2}, new QoSLevel[] { QoSLevel.MostOnce, QoSLevel.MostOnce }, (ushort)packetId++);

//payload from subscribe i receive there 
private static void Client_PublishReceivedChanged(object sender, MqttPacket packet)
            string payload = UTF8Encoding.UTF8.GetString(packet.Data);
            //Debug.WriteLine("subscribe id" + packet.PacketId); 
            Debug.WriteLine("Payload : " + payload);

but how to know which subscribe is (1,2,3 …)

I’ve not used this library but with all other libraries I’ve used on ESP32 and Android, you check the topic to know which message just arrived and then handle the payload.

yes i know with other libraries on Arduino , mbedos or ESP32 AT command it handle through subscriber(topic), but this one i want to use with TinyCLR because this libraries because is very light (that is a reason why i loved it) but only missing thing is to know under which subscribe(topic) is payload receive …

I see how they do it now. When you subscribe to a topic, you pass in a packetId and when the message received fires, you check this packetId to determine the topic its for.

A better way would be to pass the topic as part of the call back. MQTT allows you to use # as the wildcard and receive all messages and then handle the topic in the message received. With this implementation, you can’t hand any wildcards as there would be no topics to read back.

I think it worth posting about this on the drivers github to have this added.

1 Like

seems need to modify mqtt library ,to be able to retreive subcribers topic too in similiar way how was done with payload together just to include topic inside packet instead to retreive only payload values only example if i receive from subscribed topic [/home/temp] payload=23 to receive payload=/home/temp[23]