I’m publishing messages (QoS=1, 1 message/second, 0.6 KB each) from a mobile client to a subscriber web app using MQTT. Each topic will have a single publisher.
For a topic, according to the MQTT specification, the broker should ensure the delivery of messages to subscribers in the order they were received.
- Is my understanding correct?
- How can I ensure in-order packet delivery from the mobile client to the broker? For example, should I wait for a PUBACK before sending the next message? While this would ensure in-order delivery, it might introduce latency based on PUBACK packet arrival time.
- When client is offline i want to queue all the unpublished messages, and then send it when client reconnects, do the HiveMQ Java client library have built in functionality to supports this, or we need to handle this at our own.
What’s the best approach to minimize latency while ensuring in-order and lossless delivery? Additionally, how can I achieve this using the HiveMQ Java client library?
Hi @qwert477
Thank you for your questions. I’ll address each of your concerns below:
- Ensuring in-order delivery:
Your understanding is correct. The MQTT specification guarantees that messages will be delivered to subscribers in the order they were received by the broker, provided the following conditions are met:
- You are using QoS 1 or QoS 2 for message delivery.
- The same client ID and topic are used for publishing.
- The connection between the client and broker remains stable.
- PUBACK and in-order delivery:
It is not necessary to wait for a PUBACK before sending the next message, as the HiveMQ MQTT Client and HiveMQ broker support batching of messages. The number of unacknowledged messages that can be sent concurrently is determined by the maxInflightMessages
parameter on the server side. For example, in HiveMQ Cloud Serverless, this limit is set to 10. This approach minimizes latency while ensuring in-order and lossless delivery.
- Handling offline scenarios:
The HiveMQ MQTT Client library provides built-in functionality to queue unpublished messages when the client is offline. Upon reconnecting, the client will automatically retransmit the queued messages in the order they were created, ensuring no data loss. To achieve this:
- Configure the client to automatically attempt reconnections.
The client library does not enable the persistence feature to store undelivered messages locally, ensuring messages are retained if the application is restarted.
I hope this helps,
Best,
Dasha from The HiveMQ Team