# Mqtt CLI persistant session

**URL:** <https://community.hivemq.com/t/mqtt-cli-persistant-session/2605>\
**Category:** MQTT CLI\
**Created:** [January 12, 2024, 3:02pm UTC](https://community.hivemq.com/t/mqtt-cli-persistant-session/2605 "2024-01-12T15:02:19Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zuendi](https://avatars.discourse-cdn.com/v4/letter/z/ac8455/32.png) [@Zuendi](https://community.hivemq.com/u/Zuendi)\
**Post date:** [January 12, 2024, 3:02pm UTC](https://community.hivemq.com/t/mqtt-cli-persistant-session/2605/1 "2024-01-12T15:02:19Z")

</div>

Hi all,

I tried to start a persistent session using MQTT cli but couldn’t get it to work. The idea was to create a session that is able to queue a couple of messages and to receive them after my client connects again.  
Here is what I tried:  
mqtt sub -t testtopic -q 1 --no-cleanStart -i testclient -h 127.0.0.1

–no-cleanStart is the only flag I found that looks remotely like something to use to create a persistent session. But after closing the connection, the broker deletes the client out of the client list and does not queue messages.  
But maybe I overlooked something.  
Thanks in advance.  
Best regards,  
Zuendi

---

<div class="post-metadata">

**Author:** ![Daria\_H](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@Daria\_H](https://community.hivemq.com/u/Daria_H)\
**Post date:** [January 12, 2024, 3:12pm UTC](https://community.hivemq.com/t/mqtt-cli-persistant-session/2605/2 "2024-01-12T15:12:05Z")

</div>

Hi @Zuendi

You need to add Session Expiry. For example, to use Session Expiry 1h:

> [@](#):
>
> mqtt sub -t testtopic -q 1 --no-cleanStart -i testclient -h 127.0.0.1 --sessionExpiryInterval 3600

I hope it helps

Regards,  
Dasha from HiveMQ Team

---

<div class="post-metadata">

**Author:** ![Zuendi](https://avatars.discourse-cdn.com/v4/letter/z/ac8455/32.png) [@Zuendi](https://community.hivemq.com/u/Zuendi)\
**Post date:** [January 12, 2024, 3:23pm UTC](https://community.hivemq.com/t/mqtt-cli-persistant-session/2605/3 "2024-01-12T15:23:24Z")

</div>

> [@Daria\_H](#):
>
> –sessionExpiryInterval 3600

Hi Dasha,

that helps allot, thank you for this very fast response.  
I tried it out and it looks perfekt on the brokers side. The session stays open and messages are being queued.  
However after starting the client via CLI again, I get my first message and then an Error. Ideas?

> Blockquote  
> io.reactivex.exceptions.UndeliverableException: The exception could not be delivered to the consumer because it has already canceled/disposed the flow or the exception has nowhere to go to begin with. Further reading: [What's different in 2.0 · ReactiveX/RxJava Wiki · GitHub](https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#error-handling) | java.lang.UnsupportedOperationException: A publish must not be acknowledged if manual acknowledgement is not enabled  
> at io.reactivex.plugins.RxJavaPlugins.onError(RxJavaPlugins.java:367)  
> at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:69)  
> at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57)  
> at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)  
> at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)  
> at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)  
> at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)  
> at java.base/java.lang.Thread.run(Thread.java:834)  
> Caused by: java.lang.UnsupportedOperationException: A publish must not be acknowledged if manual acknowledgement is not enabled  
> at com.hivemq.client.internal.mqtt.message.publish.MqttPublish.acknowledge(MqttPublish.java:170)  
> at com.hivemq.cli.mqtt.SubscribeMqtt5PublishCallback.accept(SubscribeMqtt5PublishCallback.java:85)  
> at com.hivemq.cli.mqtt.SubscribeMqtt5PublishCallback.accept(SubscribeMqtt5PublishCallback.java:33)  
> at com.hivemq.client.internal.mqtt.MqttAsyncClient$CallbackSubscriber.onNext(MqttAsyncClient.java:303)  
> at com.hivemq.client.internal.mqtt.MqttAsyncClient$CallbackSubscriber.onNext(MqttAsyncClient.java:288)  
> at io.reactivex.internal.operators.flowable.FlowableObserveOn$ObserveOnSubscriber.runAsync(FlowableObserveOn.java:407)  
> at io.reactivex.internal.operators.flowable.FlowableObserveOn$BaseObserveOnSubscriber.run(FlowableObserveOn.java:176)  
> at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66)  
> … 6 more  
> Exception in thread “RxComputationThreadPool-1” io.reactivex.exceptions.UndeliverableException: The exception could not be delivered to the consumer because it has already canceled/disposed the flow or the exception has nowhere to go to begin with. Further reading: [What's different in 2.0 · ReactiveX/RxJava Wiki · GitHub](https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#error-handling) | java.lang.UnsupportedOperationException: A publish must not be acknowledged if manual acknowledgement is not enabled  
> at io.reactivex.plugins.RxJavaPlugins.onError(RxJavaPlugins.java:367)  
> at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:69)  
> at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57)  
> at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)  
> at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)  
> at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)  
> at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)  
> at java.base/java.lang.Thread.run(Thread.java:834)  
> Caused by: java.lang.UnsupportedOperationException: A publish must not be acknowledged if manual acknowledgement is not enabled  
> at com.hivemq.client.internal.mqtt.message.publish.MqttPublish.acknowledge(MqttPublish.java:170)  
> at com.hivemq.cli.mqtt.SubscribeMqtt5PublishCallback.accept(SubscribeMqtt5PublishCallback.java:85)  
> at com.hivemq.cli.mqtt.SubscribeMqtt5PublishCallback.accept(SubscribeMqtt5PublishCallback.java:33)  
> at com.hivemq.client.internal.mqtt.MqttAsyncClient$CallbackSubscriber.onNext(MqttAsyncClient.java:303)  
> at com.hivemq.client.internal.mqtt.MqttAsyncClient$CallbackSubscriber.onNext(MqttAsyncClient.java:288)  
> at io.reactivex.internal.operators.flowable.FlowableObserveOn$ObserveOnSubscriber.runAsync(FlowableObserveOn.java:407)  
> at io.reactivex.internal.operators.flowable.FlowableObserveOn$BaseObserveOnSubscriber.run(FlowableObserveOn.java:176)  
> at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66)

---

<div class="post-metadata">

**Author:** ![Daria\_H](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@Daria\_H](https://community.hivemq.com/u/Daria_H)\
**Post date:** [January 15, 2024, 8:39am UTC](https://community.hivemq.com/t/mqtt-cli-persistant-session/2605/4 "2024-01-15T08:39:26Z")

</div>

Hi

> [@Zuendi](#):
>
> However after starting the client via CLI again, I get my first message and then an Error. Ideas?

In order to assist you more effectively, could you please provide the specific command you are running when encountering the error? This additional information will greatly help us in diagnosing and resolving the issue promptly.

---

<div class="post-metadata">

**Author:** ![Zuendi](https://avatars.discourse-cdn.com/v4/letter/z/ac8455/32.png) [@Zuendi](https://community.hivemq.com/u/Zuendi)\
**Post date:** [January 15, 2024, 9:04am UTC](https://community.hivemq.com/t/mqtt-cli-persistant-session/2605/5 "2024-01-15T09:04:55Z")

</div>

Shure, this is what I did:  
I have 2 clients. The first sends data like this:

> mqtt pub -t sendertopic -m ‘test3’ -q 1

The second client subscribes to the topic:

> mqtt sub -t sendertopic -q 1 --no-cleanStart -i testclient -h 127.0.0.1 --sessionExpiryInterval 3600

After sending data from client 1, client 2 gets the data. Like a charm. Now I kill client 2, keep sending data and reconecct client 2 with the same command. Then the error happens. The client cannot consume the queued messages.

---

<div class="post-metadata">

**Author:** ![Daria\_H](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@Daria\_H](https://community.hivemq.com/u/Daria_H)\
**Post date:** [January 16, 2024, 8:53am UTC](https://community.hivemq.com/t/mqtt-cli-persistant-session/2605/6 "2024-01-16T08:53:44Z")

</div>

How do you kill the client? Can you share a video?

---

<div class="post-metadata">

**Author:** ![Zuendi](https://avatars.discourse-cdn.com/v4/letter/z/ac8455/32.png) [@Zuendi](https://community.hivemq.com/u/Zuendi)\
**Post date:** [January 16, 2024, 9:51am UTC](https://community.hivemq.com/t/mqtt-cli-persistant-session/2605/7 "2024-01-16T09:51:20Z")

</div>

I kill the client via ^C. Have a look:  
 ![MQTT_Cli_Error](https://canada1.discourse-cdn.com/flex035/uploads/hivemq1/original/1X/2976f980aab6c5b1a36494169d6fbf83dcb63882.webp)

---

<div class="post-metadata">

**Author:** ![Daria\_H](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@Daria\_H](https://community.hivemq.com/u/Daria_H)\
**Post date:** [January 16, 2024, 5:20pm UTC](https://community.hivemq.com/t/mqtt-cli-persistant-session/2605/8 "2024-01-16T17:20:40Z")

</div>

Hi @Zuendi,

Thank you for sharing your video!

I see that the client is currently receiving `test3` and `test4`, but unfortunately, an exception occurs thereafter. It appears that this might be a client-related issue rather than a broker one.

As a workaround, I recommend trying an alternative client such as [mosquitto\_sub](https://mosquitto.org/man/mosquitto_sub-1.html):

```auto
mosquitto_sub -t '#' -c -i CLI -q 1

```

This might provide a workaround while we investigate the underlying client bug.

If you have any further questions or need assistance, feel free to reach out.

Best regards,  
Dasha from HiveMQ Team

---

<div class="post-metadata">

**Author:** ![Zuendi](https://avatars.discourse-cdn.com/v4/letter/z/ac8455/32.png) [@Zuendi](https://community.hivemq.com/u/Zuendi)\
**Post date:** [January 16, 2024, 5:35pm UTC](https://community.hivemq.com/t/mqtt-cli-persistant-session/2605/9 "2024-01-16T17:35:36Z")

</div>

Hello Daria,

yes I agree, it should be a problem with the client, or in this case with the mqtt CLI. That’s exactly what I was concerned about. I don’t want to test another CLI, I want to improve the mqtt CLI with my hint. It is developed by HiveMQ, correct? That’s why I wanted to share my finding.

Best regards  
Zuendi.

---

<div class="post-metadata">

**Author:** ![Daria\_H](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@Daria\_H](https://community.hivemq.com/u/Daria_H)\
**Post date:** [January 16, 2024, 5:46pm UTC](https://community.hivemq.com/t/mqtt-cli-persistant-session/2605/10 "2024-01-16T17:46:45Z")

</div>

@Zuendi

I appreciate your initiative to enhance MQTT-CLI; it’s a commendable direction to take. To maximize its impact, I recommend opening an issue on the [https://github.com/hivemq/hivemq-community-edition](https://github.com/hivemq/hivemq-community-edition) repository. Ensure the issue is well-presented and informative to avoid any oversight due to formal reasons.

Thank you,  
Dasha from HiveMQ Team

---

<div class="post-metadata">

**Author:** ![Zuendi](https://avatars.discourse-cdn.com/v4/letter/z/ac8455/32.png) [@Zuendi](https://community.hivemq.com/u/Zuendi)\
**Post date:** [February 4, 2024, 11:00am UTC](https://community.hivemq.com/t/mqtt-cli-persistant-session/2605/11 "2024-02-04T11:00:20Z")

</div>

Hi Dasha,

I did this three weeks ago but do not get any answer:

> <https://github.com/hivemq/hivemq-community-edition/issues/442>
>
> \## Expected behavior
> After reopening a persistant session the MQTT CLI client s…hould process all queued data.
> \## Actual behavior
> The first queued message will be processed and then an error happens:
> \`io.reactivex.exceptions.UndeliverableException: The exception could not be delivered to the consumer because it has already canceled/disposed the flow or the exception has nowhere to go to begin with. Further reading: \[What's different in 2.0 · ReactiveX/RxJava Wiki · GitHub 1\](https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#error-handling) | java.lang.UnsupportedOperationException: A publish must not be acknowledged if manual acknowledgement is not enabled
> at io.reactivex.plugins.RxJavaPlugins.onError(RxJavaPlugins.java:367)
> at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:69)
> at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57)
> at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
> at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)
> at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
> at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
> at java.base/java.lang.Thread.run(Thread.java:834)
> Caused by: java.lang.UnsupportedOperationException: A publish must not be acknowledged if manual acknowledgement is not enabled
> at com.hivemq.client.internal.mqtt.message.publish.MqttPublish.acknowledge(MqttPublish.java:170)
> at com.hivemq.cli.mqtt.SubscribeMqtt5PublishCallback.accept(SubscribeMqtt5PublishCallback.java:85)
> at com.hivemq.cli.mqtt.SubscribeMqtt5PublishCallback.accept(SubscribeMqtt5PublishCallback.java:33)
> at com.hivemq.client.internal.mqtt.MqttAsyncClient$CallbackSubscriber.onNext(MqttAsyncClient.java:303)
> at com.hivemq.client.internal.mqtt.MqttAsyncClient$CallbackSubscriber.onNext(MqttAsyncClient.java:288)
> at io.reactivex.internal.operators.flowable.FlowableObserveOn$ObserveOnSubscriber.runAsync(FlowableObserveOn.java:407)
> at io.reactivex.internal.operators.flowable.FlowableObserveOn$BaseObserveOnSubscriber.run(FlowableObserveOn.java:176)
> at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66)
> … 6 more
> Exception in thread “RxComputationThreadPool-1” io.reactivex.exceptions.UndeliverableException: The exception could not be delivered to the consumer because it has already canceled/disposed the flow or the exception has nowhere to go to begin with. Further reading: \[What's different in 2.0 · ReactiveX/RxJava Wiki · GitHub 1\](https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#error-handling) | java.lang.UnsupportedOperationException: A publish must not be acknowledged if manual acknowledgement is not enabled
> at io.reactivex.plugins.RxJavaPlugins.onError(RxJavaPlugins.java:367)
> at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:69)
> at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57)
> at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
> at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)
> at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
> at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
> at java.base/java.lang.Thread.run(Thread.java:834)
> Caused by: java.lang.UnsupportedOperationException: A publish must not be acknowledged if manual acknowledgement is not enabled
> at com.hivemq.client.internal.mqtt.message.publish.MqttPublish.acknowledge(MqttPublish.java:170)
> at com.hivemq.cli.mqtt.SubscribeMqtt5PublishCallback.accept(SubscribeMqtt5PublishCallback.java:85)
> at com.hivemq.cli.mqtt.SubscribeMqtt5PublishCallback.accept(SubscribeMqtt5PublishCallback.java:33)
> at com.hivemq.client.internal.mqtt.MqttAsyncClient$CallbackSubscriber.onNext(MqttAsyncClient.java:303)
> at com.hivemq.client.internal.mqtt.MqttAsyncClient$CallbackSubscriber.onNext(MqttAsyncClient.java:288)
> at io.reactivex.internal.operators.flowable.FlowableObserveOn$ObserveOnSubscriber.runAsync(FlowableObserveOn.java:407)
> at io.reactivex.internal.operators.flowable.FlowableObserveOn$BaseObserveOnSubscriber.run(FlowableObserveOn.java:176)
> at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66)\`
> 
> \## To Reproduce
> 
> \### Steps
> 2 MQTT CLI Clients. 1.: Subscriber, 2.: Publisher
> 1.: Opens a persistant session
> \`mqtt sub -t testtopic -q 1 --no-cleanStart -i testclient -h 127.0.0.1 --sessionExpiryInterval 3600\`
> 2.: Sends data
> \`mqtt pub -t sendertopic -m ‘test3’ -q 1\`
> 1.: kills session via ^C
> 2.: Keeps sending Data
> 1.: reconnects
> \`mqtt sub -t testtopic -q 1 --no-cleanStart -i testclient -h 127.0.0.1 --sessionExpiryInterval 3600\`
> After the reconnect, the first MQTT CLI Client consumes the first message und throws the error.
> 
> https://github.com/hivemq/hivemq-community-edition/assets/14317353/5587e256-e196-4f16-8003-276d730d104d
> 
> \### Reproducer code
> 
> \## Details
> 
> \- Affected HiveMQ CE version(s):
> MQTT CLI:
> 4.24.0
> Picocli 4.7.0
> 
> \- Used JVM version:
> JVM: 11 (Oracle Corporation OpenJDK 64-Bit Server VM 11+28)

Best regards  
Zuendi.

---

<div class="post-metadata">

**Author:** ![Zuendi](https://avatars.discourse-cdn.com/v4/letter/z/ac8455/32.png) [@Zuendi](https://community.hivemq.com/u/Zuendi)\
**Post date:** [February 15, 2024, 7:30am UTC](https://community.hivemq.com/t/mqtt-cli-persistant-session/2605/12 "2024-02-15T07:30:28Z")

</div>

@Daria_H do you have any other idea what I can try?

---

<div class="post-metadata">

**Author:** ![Daria\_H](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@Daria\_H](https://community.hivemq.com/u/Daria_H)\
**Post date:** [February 15, 2024, 11:31am UTC](https://community.hivemq.com/t/mqtt-cli-persistant-session/2605/13 "2024-02-15T11:31:00Z")

</div>

Hi @Zuendi

Certainly! Try using [mosquitto\_sub](https://mosquitto.org/man/mosquitto_sub-1.html) command instead of `mqtt-cli sub`.

Here’s a breakdown of the `mosquitto_sub` command and its arguments:

```auto
mosquitto_sub -t '#' -q 1 -x 600 -c --id SUB --host localhost --port 1884

```

- `mosquitto_sub`: This is the command-line utility for subscribing to MQTT topics.
- `-t '#'`: Subscribe to topics using the wildcard `#`, which means subscribing to all topics.
- `-q 1`: Set the Quality of Service (QoS) level to 1. QoS defines the message delivery guarantee.
- `-x 600`: Set the session expiry interval to 600 seconds. This determines how long the broker should maintain information about the client after it disconnects.
- `-c`: Enable a persistent session (non-clean session). This means that the broker will remember the client’s subscriptions and queued messages even if the client disconnects.
- `--id SUB`: Specify the client identifier as “SUB”. This identifier is used to distinguish different MQTT clients.
- `--host localhost`: Specify the MQTT broker’s host. This is the address of the MQTT broker to which the client is connecting.
- `--port 1884`: Specify the MQTT broker’s port. This is the port number on which the MQTT broker is listening for incoming connections.

The overall command is used to subscribe to all topics (`#`) with QoS 1, set a session expiry interval of 600 seconds, use a persistent (non-clean) session, and connect to the MQTT broker with the specified client identifier, host, and port.

I hope it helps.

Best regards,  
Dasha

---

<div class="post-metadata">

**Author:** ![Zuendi](https://avatars.discourse-cdn.com/v4/letter/z/ac8455/32.png) [@Zuendi](https://community.hivemq.com/u/Zuendi)\
**Post date:** [February 15, 2024, 1:54pm UTC](https://community.hivemq.com/t/mqtt-cli-persistant-session/2605/14 "2024-02-15T13:54:54Z")

</div>

Hi @Daria_H,

you already told me that the mosquitto cli could help. But I wanted to share a problem with your mqtt cli.

> [@Zuendi](#):
>
> yes I agree, it should be a problem with the client, or in this case with the mqtt CLI. That’s exactly what I was concerned about. I don’t want to test another CLI, I want to improve the mqtt CLI with my hint. It is developed by HiveMQ, correct? That’s why I wanted to share my finding.

---

<div class="post-metadata">

**Author:** ![Daria\_H](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@Daria\_H](https://community.hivemq.com/u/Daria_H)\
**Post date:** [February 15, 2024, 3:38pm UTC](https://community.hivemq.com/t/mqtt-cli-persistant-session/2605/15 "2024-02-15T15:38:11Z")

</div>

Hi @Zuendi,

**Thank you** for bringing this issue to our attention.

Our HiveMQ engineers are on it and will investigate the problem as soon as they can. We’re committed to addressing this issue, and we aim to include the fix in one of our upcoming releases.

In the meantime, you can try using an alternative MQTT client or revert to an older version of mqtt-cli – version 4.20.0 should work for you.

If you have any more questions or need further assistance, feel free to reach out. We appreciate your patience and understanding.

Best regards,  
Dasha
