Can publisher client queue all the messages it has to publish when the connectivity to mqtt server is lost

I have a scenario where an mqtt-client (A) publishes messages to mqtt-server at a rate 100msg/sec.

Lets say if the connectivity from mqtt-client (A) to mqtt-server breaks. It is basically a TCP/IP connection. It might be because of loss of internet or any other reason.

In that case, I want mqtt-client to queue all the message within itself (may be a backup file so that even if the client restarts it can get back the queue) and publishes one-by-one when the connectivity to mqtt-server is restored.

Is it possible? If yes, how to do it? And Is there any size limitation of the mqtt-client queue?
In my case, mqtt-client means the publisher. And my use case is related to publishing, not subscribing.

Hi @Abhishek ,

Great you are interested in MQTT and HiveMQ client, welcome to the community !

Sounds like you need to implement offline message buffering. Please check out the example of offline message buffering implemented with HiveMQ MQTT Client here: Offline Message Buffering - #3 by Daria_H

I hope this helps,
Dasha from HiveMQ team

Hi @Daria_H ,

Thanks for your response.

As my use case is related to publishing, so I will focus on the below portion of the code.
mqtt5AsyncClient.publishWith()
.topic(“test1234”)
.qos(MqttQos.AT_LEAST_ONCE)
.payload(“this publish will be buffered”.getBytes(StandardCharsets.UTF_8))
.send()
.thenAccept(subAck → System.out.println(“Actually published now”));
System.out.println(“Submitted publish to client”);

I have few queries on the implementation.

  1. My publisher-client publishes 100msgs/sec. In case, the publisher-client disconnects from mqtt-broker. How much data it can save in offline buffer queue? Is there any default limit of this size? And is this limit configurable?
  2. While the publisher-client saves the message offline and publishes all the msgs when the connectivity is restored… What if the publisher-client reboots before the connectivity is restored, All the msgs will be lost? If yes, then Is there a way for reliability of msgs from publisher-client side?

@Abhishek ,

How much data the client can save should only be limited by how much memory is available to the JVM. You will need to test on your machine in your env.

For saving the data between the program restarts you usually save it to a file or a database.

I hope this helps,
Dasha from HiveMQ team