2

Is there a best practice for publishing scheduled/delayed messages with MQTT, for example, using Mosquitto or HiveMQ brokers?

The use case is: Tell a subscriber to perform some maintenance in 15 minutes.

Optimally, the use case would be then solved by publishing the message "perform maintenance now please", and mark the message with "deliver no sooner than 15 minutes from now".

augustzf
  • 2,385
  • 1
  • 16
  • 22

2 Answers2

4

While I wouldn't recommend this to do in any scenario with high throughput, at least with HiveMQ you can do the following:

  1. Implement a OnPublishReceivedCallback.
  2. Schedule a Runnable that uses the PublishService to some kind of shared ScheduledExecutorService. The Runnable re-publishes the publish via the PublishService
  3. The OnPublishReceivedCallback needs to throw away the original publish by throwing an OnPublishReceivedException (use false as constructor parameter so you don't disconnect the publishing client)
Dominik Obermaier
  • 5,610
  • 4
  • 34
  • 45
3

No, messages are delivered immediately for all connected clients subscribed to a topic and at reconnection for disconnected clients with persistent subscriptions.

If you want to do delayed messages you will have to implement your own store and forward mechanism before they are published to the broker.

hardillb
  • 54,545
  • 11
  • 67
  • 105