Search code examples
mqttmosquittohivemq

How does MQTT handle publishers simultaneously publishing to the same topic at the same time?


Imagine I have 3 publishers, A,B and C.

  • Publisher A sends messages FOO, BAZ, SOMETHING to SOME/TOPIC
  • Publisher B sends messages FOO, SOMETHING to SOME/TOPIC
  • Publisher C sends no messages to SOME/TOPIC

What messages would a subscriber to SOME/TOPIC receive?

My motivation for wanting to know this is that Publishers A,B and C are pulling these messages from a data source which is unreliable and they may miss some messages, so I have multiple publishers publishing to the same topic as a form of redundancy. Is that a good or bad idea?


Solution

  • Subscribers would see all the messages from A & B, what order they are delivered to the subscribers would depend on what order they arrived at the broker (which is going to come down to the order they arrive over the network normally) and to some extent how the broker is implemented.

    The MQTT broker spec make no mention of how to handle multiple identical messages and as such would treat them as any other message, unless you choose to create a custom broker to do differently.

    If this is a good idea or not we can't answer, as we have no idea of the consequences of the subscriber(s) receiving multiple identical messages (e.g. multiple FOO & SOMETHING messages) because the message when delivered will not contain any information about which client published it (unless you choose to encode that in the payload).