MQTT - Getting the notification when message retention period is over - mqtt

I have a requirement where I want to get some kind of notification while the message retention period is over and the message is about to be discarded from the MQTT topic.
So the actual requirement is, we have Bluetooth bands, which are send their presence through a centralized agent and an MQTT broker. Now we got a requirement where we need to upgrade the band firmware. For doing so, we will send a message to the topic with a message and a specific retention period. Infra will receive the message notification and look for the band. If the band is found then it's ok otherwise it will wait for new bands to be available. Once the retention period is over, in some cases we have to retry, so to implement the retry mechanism, I wanted to receive the notification from the MQTT broker if a message retention period is over.
Please help me if this is even possible into MQTT?

The broker won't tell you when it drops messages, but since you know when you sent the message and what expiry time you set there is nothing to stop you implementing this yourself.

Related

MQTT - How to know is message received by device

We have mqtt producer and consumer.
MQTT producer is at client level.
When we push message to producer, if the device is switched on, then it will receive the message.
If the device is switched off, then it wont receive the message until it turned on.
We need to know, when message sent to MQTT in server, if server is switched off, then we need to know the status as, it is queued or not received by server.
Based on it, we will send message for the user as, Please turn on device to do specific action.
Is there any better approach to know the status in MQTT to find is message is delivered or failed or queued to know server is active or not.
There is no end to end delivery notification in the MQTT protocol. Part of the pub/sub paradigm is that the publisher should be decoupled from the subscriber, there can be anywhere from 0 to many subscribers to a given topic.
There are 2 approaches to how to potentially work round this.
Have the subscriber respond on a separate topic to acknowledge that it has received the message. You will need to include a unique identifier in the message payload so it can be used in the response message.
You can use the Last Will and Testament feature of MQTT to have the subscriber maintain it's current status. When it starts it publishes a retained message to a known topic, e.g. publishes true to consumer/12345/status and sets a LWT to publish false if the device goes offline unexpectedly. It should also publish false if it cleanly shuts down. That way the publisher can check the status of the subscriber before deciding to publish the message.
I don't know which broker you are using. But in EMQ X MQTT broker, when QoS > 1, the message terminated delivered or ack broker will notify the server
The plugin: emqx-web-hook

Unexpected behaviour of cached messages of Paho MQTT client on Mosquitto server

I do use the PAHO C client library in my application. I do subscribe topics with MQTTAsync_subscribe() and QoS set to 1. From what I understand is that 1 means, that a message is send to the client at least one time.
I disconnect the client that subscribed the topic and the client that published the topic is still sending messages to the Mosquitto broker. If I start the subscriber lets say hours later, I get all the buffered messages beginning from the last one when the subscriber was shut down. So far so good! But the problem is, that the messages arrive in the same interval as the publisher sends new messages. By doing so, you will never get the latest message queued up by the publisher.
What I expect is that the Mosquitto broker tries to send all pending messages one after the other to the client instead of sending one old message when a new one is published.
Maybe someone can help me to understand why this happens or maybe how to overcome this situation?
Queued messages should all be batch delivered when the client reconnects.
They are not queued up and delivered with the same time offset, mosquitto wouldn't even keep time of arrival to be able to do time offset delayed delivery (Time of arrival is kept for MQTT v5 messages because they can now have TTL value, but this is only used to remove expired messages from the queue, not to delay late delivery).

MQTT paho client publish message and delete immediately the receiver consumes it

I have been trying to work with paho mqtt client to publish and receive messages with mosquitto as the broker and works fine. My use case although involves the sender publishing a message to the broker and disconnects, at this point, the receiver whether connected or disconnected should consume this message and delete it immediately. I have played with all the properties e.g QOS, retained messages, clean sessions, etc but none is yielding the result I want. Please help.
Assuming a Publish and Subscription at QOS2 the message will only ever be delivered to the subscriber once, there is nothing to delete from anywhere.
If you are trying to ensure that the message is only ever consumed by one specific client then I think you have a misunderstanding about what MQTT is.
MQTT is a PUB/SUB protocol, and as such is designed to totally decouple the subscriber from the publisher. The publisher doesn't know how many subscribers there are, just that it has published a message to a given topic.
0 to N (where N can be any number) of clients can subscribe to the topic. Using QOS, persistent subscriptions and the clean session flag, a client can indicate to the broker that it would like to receive any messages published since was last connected, but this will not influence any other clients that may have also subscribed to that topic.
Starting at MQTT protocol v5 (most brokers and clients currently still only support v3 as of Sept 2018) includes something called Shared Subscriptions* that can be used to round-robin deliver messages on a give topic to a group of clients so only 1 of the set will receive this message, but this does not prevent clients not part of the group from also receiving the message.
The last message with the retained flag set published to a topic will be delivered to all clients at the point they subscribe to the topic. This message can be cleared by publishing a new message with a null payload and the retained flag set. A client could publish a message like this as soon as it receives the retained message but there would still be a timing window where other clients may subscribe and receive the retained message.
*some v3 brokers have implemented propriety versions of this.

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 messageId practical implementation

The company I am working for has evaluated MQTT and decided to use it as a core messaging platform for a large scale system. The main reason is how compact the protocol is and how easy it can actually be implemented. I have a single issue with MQTT though and I'm seeking for an answer to the following question:
QoS1 and QoS2 messages require confirmation from the client. The only thing I know about the message (identifying it) when receiving PUBACK, PUBREC, PUBREL and PUBCOMP is messageId and the clientId. Message id is an unsigned int16 so the max value is 65535. It doesn't seem to be large enough for long running clients, say a year, sending 15 QoS2 messages an hour.
I am not quite sure if there's any other way to identify the message? I would like to be as compliant with the standard as possible.
Probably the first point to make clear is that message IDs are handled on a per client and per direction basis. That is to say that the broker will create a message ID for each outgoing message with QoS>0 for each client that is connected and these message IDs will be completely independent of any other message IDs used for the same message published to other clients. Likewise, each client generates its own message IDs for messages that it sends.
The message ID doesn't have to be unique, so your client sending 15 messages per hour with QoS level 2 would simply overflow at some point. The real limitation is that there can only be a maximum of 65535 messages per direction "in flight" at once (i.e. part way through the message handshake). Once a message with a given ID has been fully processed then that message ID can be reused.
Another way of looking at it is to consider how it would work if your client only ever had one message in flight at once, whether because of the rate the messages are being transmitted or by design in the way you handle the messages. In this case, you could keep message ID set to 1 for every single message because there is never a chance that there will be a duplicate.
If you wish to support having multiple messages in flight at once it would be relatively straightforward to check there are no message ID duplicates before you assign a new one.
Because the message ID is per client, if you send a single message to >65535 clients there will be no chance of message ID collisions. If you send >65535 messages to each client at once and the message flows aren't complete then there will be problems.
Answering the comment "I have noticed that every MQTT broker tends to deliver only the last QoS1/2 message":
The broker will only send messages to clients it knows about. If you connect for the first time there is no way to get messages from the past, with one exception: retained messages. If a message is set to retained then it is a "last known good" value. When a new client subscribes it will be sent the retained message immediately which makes it useful for things that are updated infrequently. I suspect this is what you are referring to. If you want a client to have messages queued when it is not connected then you must connect with the "clean session" option disabled to make the client persistent. You must also use QoS>0 subscriptions and QoS>0 publications. When your client reconnects (with clean session still set to disabled), the queued messages will be delivered. You can normally configure the number of messages to queue in this way in the broker, where any further messages will be discarded. An important point is that queueing messages for a client that has not previously connected is not supported by design.
For delivering more messages at QOS1 or QOS2, you should use concept of persistant memory. In this when ever a subscriber is not available the message get stored in persistant memory and deliver once subscriber is connected. You can do this at QOS0 also after configuring mosquitto.conf file.

Resources