using paho python client to test mosquitto over websockets

Asked by leo

Hi,
i can use paho python client to pub/sub with local broker on port 1883 or 8883.
but when i change to test websockets on its port 8080, there is no return for python script.
i use example code to test common broker, test.mosquitto.org, same result.
am i wrong somewhere? for mosquitto over websockets, which paho client can be used? js only?

example.py :

#!/usr/bin/python
import sys
import paho.mqtt.client as mqtt

def on_connect(mqttc, obj, flags, rc):
    print("connect 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("Published mid: "+str(mid))

def on_subscribe(mqttc, obj, mid, granted_qos):
    print("Subscribed: "+str(mid)+" "+str(granted_qos))

mqttc = mqtt.Client()
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("$SYS/#", 0)

mqttc.loop_forever()

Question information

Language:
English Edit question
Status:
Solved
For:
mosquitto Edit question
Assignee:
No assignee Edit question
Solved by:
Roger Light
Solved:
Last query:
Last reply:
Revision history for this message
leo (chleoxu99) said :
#1

can anyone help on this question?

Revision history for this message
leo (chleoxu99) said :
#2

in the doc of paho-mqtt 1.1 https://pypi.python.org/pypi/paho-mqtt#connect-reconnect-disconnect
it writes:
"The connect() function connects the client to a broker. This is a blocking function. It takes the following arguments:
...
port
the network port of the server host to connect to. Defaults to 1883. Note that the default port for MQTT over SSL/TLS is 8883 so if you are using tls_set() the port may need providing manually
...

so i assume mqttc.connect("test.mosquitto.org", 8080, 60) doesn't work. just as i tested, the func had no return.
then my question is how i can connect the broker on its port 8080 on RPi, no GUI, better to use python script?
thanks

Revision history for this message
Best Roger Light (roger.light) said :
#3

Your original guess is correct, the Python client does not support websockets as a transport, it only supports direct TCP.

You could either modify the Python client to support websockets, or use a client that supports it - the javascript client is the obvious choice.

Revision history for this message
leo (chleoxu99) said :
#4

Thanks Roger Light, that solved my question.