MQTT messageId practical implementation - mqtt

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.

Related

Broadcast to only one web socket in room using ActionCable

I am using ActionCable to provide Browser Notification which is easy to implement. The problem is coming when a user has open the multiple tabs in the browser and I need to send the data to only one tab i.e to only one WebSocket in user room. How can this be done?
Isolating a single pub/sub client isn't directly possible with the pub/sub approach, because the whole idea is that publishers have no knowledge of subscribers.
However, there are two common ways to solve this:
to use a different named channel per connection, saving the named channel in the database and forwarding all messages to that specific named channel (i.e. saving the channel user-ID-TIMESTAMP in the database and using it as the target connection).
another, somewhat more reliable approach (though more complex) is to send the message to all clients but create a race condition that allows only a single client to receive the actual message. This would look something like this:
server sends "you have a message" to all clients.
client polls "undelivered" messages from the server.
server locks the message pool or uses a database transaction in order to retrieve undelivered messages and mark the messages as delivered. The server sends the undelivered messages to the client (optionally setting an ACK timeout).
a single client connection receives the undelivered messages the rest get an empty array of messages (since they were all delivered to the other client) or receive a "delivered" flag so the data is updated but no notification is raised.
(optional) the client sends and ACK.
(optional) the server marks the message delivery as complete. If no ACK was received before "timeout", server unmarks delivery and resends the "you have a message" message.
Good luck!
Every time a new connection is made we will create a new room. So for example when the user is making a new connection we can give the room name as users:user_id:some_unique_random_string which may be equal to users:user_id:123j123b1h2b1j23bh12b3 and when the same user makes another connection by opening another tab we will also do the same and create a separate room.
Now one thing the ActionCable provides is that we can find all the room name followed by any prefix.
Lets the user has made three connections and their rooms are users:128:123n1jh123ko9876, users:128:asdas23412cs1234, users:128:asni9202h5i3jens then we can obtain these room name using ActionCable also.
user_id = 128
pubsub = ActionCable.server.pubsub
channel_with_prefix = pubsub.send(:channel_with_prefix, RoomChannel.channel_name)
channels = pubsub.send(:redis_connection).pubsub('channels', "#{channel_with_prefix}:users:#{user_id}:*")
Now the channels is an array consisting of the rooms name.
So puts channels
["chatapp_production:users:128:123n1jh123ko9876", "chatapp_production:users:128:asdas23412cs1234", "users:128:asni9202h5i3jens"]
This is how we can find all the rooms related to a single user without using any external database and API calls.

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.

Duplicate Lifecycle Events

I am building a web app that uses AWS IoT lifecycle events and logs device connection/disconnection.
Using AWS IoT rules, I am sending all events to a lambda and after some validation I'm saving all lifecycle events to a DynamoDB table. I'm aware that messages may be delayed, out of order and duplicates may happen.
I am validating for all these scenarios, so my connection log is as accurate as possible.
My question is: Is it possible for duplicate messages to come with a distinct timestamp? Such as a disconnection being sent twice with the same sessionIdentifier but a different timestamp?
Just some guesses
MQTT QoS 1 implies the "You might receive duplicate messages." thing. The message could be resend by one side if no ack is received from the other side. Thus, it is the same old message, and the timestamp would not change.
The timestamp field refers to the time the event occurred, not the time the message is sent. Thus, it should remain the same value.
Reference: http://docs.aws.amazon.com/iot/latest/developerguide/life-cycle-events.html

MQTT know if a client is subscribed (cont)

Today I posted this topic
MQTT know if a client is subscribed
I would like to try something, but I would like to know if is feasible, may be I'm complicating the things, so if you have another suggestion please feel free
let's suppose that we have the topic temperature/subscription
First
Client A subscribes to subscription (or in other words temperature/subscription)
if he doesn't receive a payload (a retain message)
then
Client A publishes to subscription with a payload of 1 or "online" as a retain message
and subscribes to the temperature topic
Now, Client B
again, subscribes to subscription if he receives a payload = someone is already subscribed (Client A)
So, he can't subscribe to temperature topic
I still need to add the LWT message, but first I want to start with something simple.
Is it feasible to do this? so I this way I can see if someone is subscribed or not to a topic
As we discussed last time, that will work, but it has a huge gaping timing window between checking for a message on temperature/subscription and publishing a message to it to claim the subscription.
Also you need to use the client id of as the marker because retained messages will survive a broker restart, which will disconnect all the client, so client A won't know it still holds the "lock" after a broker restart.
The LWT is only there for is client A crashes and then it will wait for the TTL for the connection to expire before sending the message, you need to explicitly clear that topic if client A disconnects cleanly.
In summary this is a really bad idea, trying to retro fit a concept that MQTT is not suited to.

Receive offline messages mqtt

I'm using mosquitto as broker and paho(python) as client. I'm trying to make subscriber to receive offline messages.
For that I made following changes:
Fixed client ID
qos level 2
but, still the subscriber is not able to receive messages.
any help?
Thanks,
Rahul
In order to have your client as a durable client and receive messages that were sent to topics when it was offline, you need to meet the following criteria:
Fixed client ID (as you've done)
Always connect with clean_session=False
Subscriptions must be made with QoS>0
Messages published must have QoS>0
The mistake that I make most frequently is to forget either one of points 3 and 4, so I'm publishing with QoS=0 or subscribing with QoS=0, either of which would cause messages not to be stored.
You could also look at the queue_qos0_messages option to tell the broker to store QoS=0 messages as well. Note that this is an implementation detail that may be specific to mosquitto.
Check if you have set the retain flag to true when publishing message to topic, with retain=true, new connected client which subscribes the topic will receive the retained message.

Resources