Between Feb and now the paho.mqtt.python codebase has been fixed. Just add transport='websockets'
as Fl0v0 suggested, it simply works. Below is the full code which subscribes to everything or everything under $SYS/
. The code tested is on the master branch with commit hash e56f913
on June 3 of 2016.
#!/usr/bin/python
import sys
import paho.mqtt.client as mqtt
def on_connect(mqttc, obj, flags, rc):
print("rc: "+str(rc))
def on_message(mqttc, obj, msg):
print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload))
def on_publish(mqttc, obj, mid):
print("mid: "+str(mid))
def on_subscribe(mqttc, obj, mid, granted_qos):
print("Subscribed: "+str(mid)+" "+str(granted_qos))
def on_log(mqttc, obj, level, string):
print(string)
mqttc = mqtt.Client(transport='websockets')
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_publish = on_publish
mqttc.on_subscribe = on_subscribe
mqttc.connect("test.mosquitto.org", 8080, 60)
mqttc.subscribe("#", 0)
#mqttc.subscribe("$SYS/#", 0)
mqttc.loop_forever()
Update: The code above does not work on Windows 7 with python 2.7.6 that I tested with, by invoking from one version of Cygwin (not the cygwin python though). None of the topic "#"
or "$SYS/#"
gives any published message back.
Instead, pub or sub to solid topics works as the example follows. Patch paho.mqtt.python examples/pub-single.py
and examples/sub-simple.py
. Then run sub-simple.py
in one terminal. Run pub-single.py
in another terminal, twice. The subscriber terminal will print out two messages published by the publisher.
Patch to examples/pub-single.py
:
-publish.single("paho/test/single", "boo", hostname="test.mosquitto.org")
+publish.single("/HelloWorld", "boo", hostname="test.mosquitto.org",
+ port=8080, transport='websockets')
Patch to examples/sub-simple.py
:
-topics = ['#']
+topics = ['/HelloWorld']
-m = subscribe.simple(topics, hostname="iot.eclipse.org", retained=False, msg_count=2)
+m = subscribe.simple(topics, hostname="test.mosquitto.org",
+ retained=False, msg_count=2,
+ port=8080, transport='websockets')