RabbitMQ Bridge

TIP

After EMQX version 3.1, a powerful rule engine is introduced to replace plug-ins. It is recommended that you use it. See Bridge data to RabbitMQ to setup RabbitMQ bridges in rule engine.

EMQX bridges and forwards MQTT messages to RabbitMQ cluster:

image

Config file of RabbitMQ bridge plugin: etc/plugins/emqx_bridge_rabbit.conf

Configure RabbitMQ Cluster

  1. ## Rabbit Brokers Server
  2. bridge.rabbit.1.server = 127.0.0.1:5672
  3. ## Rabbit Brokers pool_size
  4. bridge.rabbit.1.pool_size = 4
  5. ## Rabbit Brokers username
  6. bridge.rabbit.1.username = guest
  7. ## Rabbit Brokers password
  8. bridge.rabbit.1.password = guest
  9. ## Rabbit Brokers virtual_host
  10. bridge.rabbit.1.virtual_host = /
  11. ## Rabbit Brokers heartbeat
  12. bridge.rabbit.1.heartbeat = 30
  13. # bridge.rabbit.2.server = 127.0.0.1:5672
  14. # bridge.rabbit.2.pool_size = 8
  15. # bridge.rabbit.2.username = guest
  16. # bridge.rabbit.2.password = guest
  17. # bridge.rabbit.2.virtual_host = /
  18. # bridge.rabbit.2.heartbeat = 30

Configure RabbitMQ Bridge Hooks

  1. ## Bridge Hooks
  2. bridge.rabbit.hook.client.subscribe.1 = {"action": "on_client_subscribe", "rabbit": 1, "exchange": "direct:emq.subscription"}
  3. bridge.rabbit.hook.client.unsubscribe.1 = {"action": "on_client_unsubscribe", "rabbit": 1, "exchange": "direct:emq.unsubscription"}
  4. bridge.rabbit.hook.message.publish.1 = {"topic": "$SYS/#", "action": "on_message_publish", "rabbit": 1, "exchange": "topic:emq.$sys"}
  5. bridge.rabbit.hook.message.publish.2 = {"topic": "#", "action": "on_message_publish", "rabbit": 1, "exchange": "topic:emq.pub"}
  6. bridge.rabbit.hook.message.acked.1 = {"topic": "#", "action": "on_message_acked", "rabbit": 1, "exchange": "topic:emq.acked"}

Forward Subscription Event to RabbitMQ

  1. routing_key = subscribe
  2. exchange = emq.subscription
  3. headers = [{<<"x-emq-client-id">>, binary, ClientId}]
  4. payload = jsx:encode([{Topic, proplists:get_value(qos, Opts)} || {Topic, Opts} <- TopicTable])

Forward Unsubscription Event to RabbitMQ

  1. routing_key = unsubscribe
  2. exchange = emq.unsubscription
  3. headers = [{<<"x-emq-client-id">>, binary, ClientId}]
  4. payload = jsx:encode([Topic || {Topic, _Opts} <- TopicTable]),

Forward MQTT Messages to RabbitMQ

  1. routing_key = binary:replace(binary:replace(Topic, <<"/">>, <<".">>, [global]),<<"+">>, <<"*">>, [global])
  2. exchange = emq.$sys | emq.pub
  3. headers = [{<<"x-emq-publish-qos">>, byte, Qos},
  4. {<<"x-emq-client-id">>, binary, pub_from(From)},
  5. {<<"x-emq-publish-msgid">>, binary, emqx_base62:encode(Id)}]
  6. payload = Payload

Forward MQTT Message Ack Event to RabbitMQ

  1. routing_key = puback
  2. exchange = emq.acked
  3. headers = [{<<"x-emq-msg-acked">>, binary, ClientId}],
  4. payload = emqx_base62:encode(Id)

Example of RabbitMQ Subscription Message Consumption

Sample code of Rabbit message Consumption in Python:

  1. #!/usr/bin/env python
  2. import pika
  3. import sys
  4. connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
  5. channel = connection.channel()
  6. channel.exchange_declare(exchange='direct:emq.subscription', exchange_type='direct')
  7. result = channel.queue_declare(exclusive=True)
  8. queue_name = result.method.queue
  9. channel.queue_bind(exchange='direct:emq.subscription', queue=queue_name, routing_key= 'subscribe')
  10. def callback(ch, method, properties, body):
  11. print(" [x] %r:%r" % (method.routing_key, body))
  12. channel.basic_consume(callback, queue=queue_name, no_ack=True)
  13. channel.start_consuming()

Sample of RabbitMQ client coding in other programming languages:

https://github.com/rabbitmq/rabbitmq-tutorialsRabbitMQ Bridge - 图2 (opens new window)

Enable RabbitMQ Bridge

  1. ./bin/emqx_ctl plugins load emqx_bridge_rabbit