Search code examples
streamrabbitmqmqttamqp

Rabbitmq: Listening in MQTT


Here I will explain my desired used case:

  • I create a durable queue for a mqtt client (mqtt-subscription-client1qos1) before the device connect to the broker. Exchange (amp.topic)
  • I create some bindings for that queue (topic1, topic2)
  • I send 1 message to topic1 (using amqp)
  • I send 1 message to topic2 (using amqp)
  • Then using MQTT client setting client="client1" I connect to "topic1" using the same exchange (amp.topic)

How many messages I suppose to get? I expect 1, but I am getting 2 , and I am not sure I am testing right or if this is the expected beheavior.

Thanks for the help in advance


Solution

  • In AMQP, which RabbitMQ is engineered around, messages are always published to an exchange, routed according to routing keys and bindings and consumed from a queue.

    In MQTT, messages are published to the broker, routed according to topics and consumed via a subscription.

    The RabbitMQ MQTT plugin maps these concepts together:

    • The MQTT broker is emulated by a single exchange
    • The MQTT topic is used as a routing key
    • The MQTT subscription is a binding from that exchange to a queue
    • Finally, each MQTT client has a queue from which it can receive its subscribed messages

    The key thing here is that these are just ordinary RabbitMQ exchanges and queues, used to emulate the MQTT model - messages can be routed by the broker to any queue, and messages from other brokers can be routed to the queue, and they don't need to relate to the topics you've defined in MQTT.

    So if you manually route a message into the queue for a particular MQTT client, RabbitMQ will deliver it to the client along with other messages in that queue. The "topic" that the client is subscribed won't make any difference, because that is used only to set up the binding on the server.