There are plug-ins to implement MQTT in RABBITMQ, refer to the following links for details
Http://www.rabbitmq.com/mqtt.html
Installing plugins
According to the official website, as long as the RABBITMQ Mqtt plugin is enabled:
Rabbitmq-plugins Enable Rabbitmq_mqtt
When enabled, restart Rabbitmq-server.
--python implementation of MQTT messaging
Among the many implementations of MQTT broker, Mosquitto (mosquitoes) is a well-known one.
As described in the link below, RABBITMQ's Mqtt plugin is interoperable with mosquitto (interoperability)
http://www.rabbitmq.com/blog/2012/09/12/mqtt-adapter/
Acquisition of the Python client library
First, you need to download the Python client library, which can be downloaded to Mosquitto from the link below.
http://mosquitto.org/download/
After the download is complete, the Python library is installed in the./lib/python directory under the source directory.
Python setup.py Build
Python setup.py Install
Messaging implementation
Download the Mosquitto source directory above./lib/python, there is an example to implement the sub.py
http://mosquitto.org/documentation/python/
The above link has a description of how to send and receive messages in Python, and I'll summarize it a little bit.
To implement a message receipt, there are a total of the following steps
1. Create client, register callback function
2. Subscribe to messages (set topic)
3. Cyclic processing
When a message is subscribed (function prototype: subscribe (self, topic, qos=0)), it provides the topic to subscribe to the message, and the expected quality of service (https://www.ibm.com/developerworks/cn/ Webservices/ws-mqtt/index.html, which is described in the connection as ' at least once ', ' one at a time ', ' only once ' of the three quality of service.
When an event is encountered in the loop processing, the callback function that is registered at the beginning is called. According to the introduction of the Web page, the following types of events:
Connect Callback
Disconnect Callback
Publish Callback
Message Callback
Subscribe Callback
Unsubscribe Callback
For the above callback introduction, there are more detailed instructions in the link, please refer to.
In this client implementation, the loop () method invocation is very important, as explained in the introduction, the loop method is to handle the incoming and outgoing network data, and need to be called frequently. The link says that the function has a parameter that specifies the number of milliseconds to wait (the default is 1), but the prototype in the Help is: loop (self, timeout=1.0, Max_packets=1), and timeout is the number of seconds.
What exactly is a prototype? Whatever the loop is doing, the description in Help says that loop invokes select to listen for the socket event, and the loop handles the received data (by invoking the callback function defined above), and for the data sent by the sending function, Loop to the aftermath. Here is the help description for loop:
This function must was called regularly to ensure communication with the broker was carried out. It calls Select () on the network sockets to wait for network events. If incoming data is present it'll then be processed. Outgoing commands, from e.g. publish (), was normally sent immediately that their function was called, but this was not alway s possible. Loop () would also attempt to send all remaining outgoing messages, which also includes commands that is part of the flow F or messages with qos>0
In the sub.py example given in the official website, the Loop_forever () function is finally called, which is described in the help of this function:
This function, call loop (), is a infinite blocking loop. It is useful for the case where you are only want to run the MQTT client loop in your program.
Loop_forever () would handle reconnecting for you. If you call disconnect () in a callback it'll return.
The Loop_forever () method can help you to call the loop method, and it is very convenient for you to do the reconnection processing. If Loop,loop is not called through the method, it returns after one session (this is the test).
Code:
sub.py the following, I added a note (in fact, the comments are already very detailed), from this point can be seen in the use of Mqtt client method
# import Mosquitto Python library import Mosquitto # define your own callback function Def on_connect (mosq, obj, RC): Mosq.subscribe ("$SYS/#", 0 ) Print ("RC:" +STR (RC)) def on_message (mosq, obj, msg): Print (msg.topic+ "" +str (Msg.qos) + "" +str (msg.payload)) d EF on_publish (mosq, obj, mid): Print ("Mid:" +str (mid)) def on_subscribe (Mosq, obj, Mid, Granted_qos): Print ("Subs Cribed: "+str (mid) +" "+str (Granted_qos)) def on_log (mosq, obj, Level, string): Print (String) # Create client # If you want t o use a specific the client ID, use # MQTTC = Mosquitto. Mosquitto ("Client-id") # but note the client ID must is unique on the broker.
Leaving the client # id parameter empty'll generate a random ID for you. MQTTC = Mosquitto. Mosquitto () # Register callback function mqttc.on_message = on_message Mqttc.on_connect = On_connect mqttc.on_publish = On_publish Mqtt C.on_subscribe = On_subscribe # Uncomment to enable debug messages #mqttc. On_log = on_log # Connect to Server Mqttc.connect ("Test.mos Quitto.org ", 1883, 60) # Subscribe to message #mqttc. suBscribe ("string", 0) #mqttc. Subscribe (("tuple", 1)) #mqttc. Subscribe ([("List0", 0), ("List1", 1)]) # Infinite loop processing Mqttc.loop_f Orever ()
To get a clearer idea of the middle principle, consider the MQTT protocol itself (a lightweight protocol, not very difficult), and read the Mosquitto source (2000 lines in total).