hasew
hasew

Reputation: 23

is there a way of storing the undelivered messages for a particular client?

I'm trying to implement a communication method between client and admin (bidirectional) using Paho MQTT JS + Mosquitto broker 1.6.8 and I'm having some trouble with retained messages and persistent sessions.

What I want to accomplish: My web client is subscribed to a topic where the admin publishes (let's say topicA) and the admin is subscribed to a topic where the client publishes (topicB). When any of the users received a message for that topic, it appears on the screen, and they keep appearing below the previous so that they form a list of messages. When they are both online, they have to see the messages that are being sent and if they go offline, they have to see the old and new messages as well. Also, the users must have the ability to clear the list of messages.

What I've tried so far: My first try was setting the published messages with the retained value to true, so that the message can be delivered to future subscribers. However, I see that only the last message is retained. I investigated and found that I can establish a persistent session (by cleanSession: false) between broker and client, so that if I set the qos of the message to a value greater than 0 (0 or 1) and the client is subscribed to the topic, they'll get all the undelivered messages. This doesn't work for me, or at least the way I expect it to work. I've tried removing the Mosquitto Db and restarting (didn't change anything). I also tried using the same id for all clients and another id for all admins, because I thought that maybe, the broker saves the messages only for a particular clientID, so if a client with a completely different ID connects, they won't get the messages.

Is there a way of delivering ALL the undelivered messages (because of one client being online and the admin offline, or viceversa) and not only the las one (retained)? Or is there a way of retaining more than one message (better in my opinion, if I could clear the retained ones by sending a null payload) ?

Upvotes: 0

Views: 283

Answers (1)

hardillb
hardillb

Reputation: 59608

First every client must have a unique client id. When there is a client id clash the oldest connected client will be disconnected.

Second it's the QoS of the subscription, not the publish that dictates if the broker will queue a message for a offline client with a persistent session

So for a client to receive messages published while it is offline, it must

  1. Have previously connected and subscribed to the topic with a QoS of 1 or 2
  2. When reconnecting it must use the same client id as last time
  3. Have cleansession set to false

The messages will only be delivered once so there is no need to clear the messages, but if you don't have to receive the queued messages, set the cleansession flash to true.

The broker will only retain the last message published on a given topic that had the retained bit set, there is no way to change this.

Upvotes: 0

Related Questions