ESP32 subscription problem to the MQTT topic - mqtt

I used two ESP32 boards one having the publish code and the other one having the subscribe code.
The publish code ESP32 publishes the data to the topic - room 1. And the subscriber esp32 code subscribes to the room 1 topic.
Data is published from the esp32 side with no problem but at the subscriber end the esp32 unable to get the data. it automatically gets disconnected and reconnects whenever the data is published or trying to get the data.

Related

MQTT QOS 2 with multiple QOS 2 subscribers

I am using MQTT V5 in my project and I have a business requirement as follows:
“Publisher shall receive acknowledgment from the subscribers confirming that they have successfully received the publisher’s message”.
Notice that this IS NOT AT ALL the same requirement satisfied by the MQTT request/response pattern introduced in MQTT V5. My publisher DOES NOT need a traditional proper response with data in it. My publisher only needs a receipt acknowledgment to know that its message was received by the subscribers. That’s it, the lighter the communication packets are the better it is for me.
I read the following article:
https://www.emqx.io/blog/introduction-to-mqtt-qos
which includes the following sequence diagram for QoS 2:
I fully understand the acknowledgment sequence when there is only one subscriber as shown in the diagram.
My questions are:
In the case where there are multiple subscribers to the publisher’s topic, ALL of them with QoS 2 subscriptions:
1 - will my publisher receive one PUBCOMP acknowledgment for each subscriber? In another words, will my publisher receive multiple PUBCOMP acks from the broker.
2 - or will the broker send only one PUBCOMP message to my publisher only after it has successfully received PUBCOMP acknowledgments from ALL subscribers? In another words, will the broker send only one PUBCOMP ack to my publisher only after it has successfully delivered the message to all subscribers?
Thanks in advance for you attention to this question.
High QOS is only between one client and the broker at a time, not end to end delivery.
That diagram breaks down if there is an offline client with a persistent subscription to a matching topic. If that was the case with the given diagram then the client would not receive PUBCOMP until that client comes back online (which could be never). This would also mean that it could never publish another message as at QOS2 there can only be one message in flight at a time.
That diagram can not be correct. (It also has the broker storing the message before it's been delivered from the publishing client). It also doesn't handle what happens if the subscribing client subscribes at QOS0 or QOS1

How to know the remote device is online using mqtt

How to know the remote device is online using mqtt (as a device simulator I am using MQTTfx). After publishing to a topic I am not getting any acknowledgement message and in the callbacks I am getting the same payload data that is published.
You can not find out is another MQTT client is online at a protocol level, in fact at a Pub/Sub pattern level a publishing client should not care is 0 or an infinite number of other clients are subscribed to the topic that message is published on.
The callbacks are only an indication that the message has been successfully delivered to the broker not to any of the subscribers. There is no end to end delivery notification (because there may be no subscribers to a given topic).
If you need to know the status of devices then you need to implement this yourself, the usual pattern is to use a status topic with a retained message and the LWT to clear this if the client crashes and setting it manually when it starts up or shuts down cleanly.

MQTT broker communication to MQTT Client

I already have a cumulocity client that communicates with the cumulocity broker through MQTT.
What should I do in order to send data back from MQTT broken in cumulocity to mqtt client? (Say the client sends some data and I want the confirmation that the data was sent successfully)
For some reason couldn't find any info on this on the cumulocity docs the only for client.
If you want to get confirmation from the server of getting your data you should use the normal MQTT QoS. http://cumulocity.com/guides/mqtt/implementation/
If you want to send data in general from the platform to your device client operations is what you are looking for. This is currently the only data you can subscribe to on Cumulocity MQTT.
http://cumulocity.com/guides/concepts/domain-model/#operations
You can check the python example. It contains the subscription part
http://cumulocity.com/guides/mqtt/hello-mqtt-python/
You should connect to the broker with Qos 1 or above. This will guarantee that the data has reached the broker at least once. The client will be receiving the PUBACK message once this happens. If the connectivity is lost then the client is supposed to re-send the PUBLISH message with Duplicate flag set. So the cient should stop publishing when a PUBACK is received.
For more information about Qos, refer this link
HiveMQ/blog/mqtt-essentials/QualityOfService

How to publish a message to a specific client in Mosquitto MQTT

Currently we have an app that controls multiple devices through MQTT. Each device subscribes to a unique topic which is named after their device ID. For example, device A has device ID 123 so it will subscribe to topic 123. Then if the app wants to publish control message to device A, then it will publish a topic named 123, which is the device ID of device A.
By doing this, if we have 1000 devices then we will have 1000 topics. This is a really bad design. So we are thinking that maybe we can publish a topic to a specific client by setting the client ID that will receive the topic since each client that connects to the broker will have to set a client ID. However, we did not find any method that allows publishing to a specific client. And it seems that MQTT doesn't handle such thing. It only publishes to clients subscribing to the same topic.
So, is there any other way that we can do to to achieve one topic, but still able to publish message to specific clients?
Thanks!
There is no way to publish a message to a single subscriber at the MQTT protocol level.
One of the key tenets of a publish/subscribe system is to totally decouple the publisher from the subscribers, there is no way for a publisher to know if there are any subscribers to a given topic let alone target one specifically.
Using a topic for each device is not a problem as there is virtually no overhead in the broker for each topic. You can also use ACLs to ensure that each client can only subscribe their own topic (while still being able to publish to others if needed)
You could use a single topic that all clients subscribe to and encode the target device in the payload and have the device decide if the message is for it's self. One downside to this is that you can't apply ACLs to this model. Another downside is increased network traffic, since uninteresting messages will be sent to many subscribers.
We have managed this within the Mosca MQTT Broker, that is not on protocoll level, but it works fine, by a procedure that the clients send in the course of the subscription a message, containing an options, which controls the forwarding of logging (channel "logger") messages
{"login":{"UserName":"John_Doe","option":["receive_logs"]}}
Retrieve login information and
let LoggedInUsers = {};
server.on('published', function(packet, client) {
let contentstr = packet.payload.toString();
//parse Package Content
let packagecontent = JSON.parse(contentstr);
if (packagecontent.hasOwnProperty('login'))
if (!LoggedInUsers.hasOwnProperty(client.id))
LoggedInUsers[client.id] = packagecontent.login;
}
Provide an individual "authorizeForward", checking if the user is supposed to receive the relevent contents
let authorizeForward = function(client, packet, callback) {
if (packet.topic === "logger" && LoggedInUsers.hasOwnProperty(client.id))
if (LoggedInUsers[client.id].option.indexOf('receive_logs') === -1)
return;
callback(null, true);
};
The next step will be to request the data from a server and read the rights from there...

MQTT: Message queuing at server side

I am using mqtt to implement one of the kind of email notification system. I am also planning to use it for trigger notifications inside the webapp. I am confused between whether MQTT stores data at server side itself when we throw on MQTT url with publisher id in JSON format? The reason i am asking this is because in my case, the MQTT stores only the last thrown data, if i send another one then the previous one get disappeared. I want to know is it present at MQTT side from birth(as MQ stands for Message queuing) & i haven't used or need to be implemented at server/consumer side?
There is a common error on Internet ... MQTT stands for MQ Telemetry Transport and not Message Queueing Telemetry Transport. It was created by IBM (with Eurotech) and it was part of the MQ products family in IBM.
MQTT hasn't queue. The broker receives the message on a topic and forwards it on all subscribers on that topic.
There are two main variations on this behaviour:
If the publisher send a message with the "retain" flag active, the broker store this message (only this). If a client subscribes to that topic, the broker immediately sends this last storage message. It's so called "last known message"
If the subscriber connects to the broker with "clean session" to false, the broker saves all subscriptions and all messages only if the client is offline. It's like a queue but not a very queue. With "clean session" false, if the client goes offline but some publishers send messages to topic it is subscribed, the broker store these messages. When the client returns online, it will receive all messages lost.
Paolo.

Resources