简体   繁体   中英

Publish MQTT message via javascript

I'm trying to publish a MQTT message via javascript using the Paho MQTT js client:

gClient = new Paho.Client("192.168.178.34", 9001, "clientId");
gClient.send("mytopic", "myvalue", 1, true);

As I can see in another MQTT client, the message is actually sent. But strangely the QOS is 0 (not 1) and the message is NOT retained.

According to the documentation https://www.eclipse.org/paho/files/jsdoc/Paho.MQTT.Client.html it should work. Why doesn't it?

Firstly the QOS is only between the a client and the broker, not between the 2 clients, so even if the message is published at QOS 1, if the client has subscribed at QOS 0 that is what the the client will see.

Secondly I'm 99% sure you will only see the retained bit set in the other client when the message is delivered as part of the response to a new subscription, eg if you publish the message with the retained bit set, then subscribe a new client to that topic. If a client is already connected then they won't see the retained bit set as it's just a normal delivery at that point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM