Python mqtt over websockets


I am having trouble sending out MQTT requests overs my company’s proxy. I believe using websockets over port 80 would solve this issue. I am currently using the losantmqtt library for python current, but how can I go about changing the source to use websockets.



The Losant MQTT Python library doesn’t have support for websocket based connections at the moment. Looks like the underlying MQTT library we use does support websockets (see the transport option) - so would just need to thread that up through as an option for the Losant client. I’ll file a ticket for us to add that internally, but if you don’t want to wait, we would love if you contributed a pull request against our client!


ah I see. I saw something in the javascript library where it gives the option to change the transport option. Could you confirm this? If thats the case, I may convert my pyhton code to javascript code


Yup! See the transport option here. I would recommend using wss (secure websockets, over port 443) as opposed to ws (regular websockets, over port 80), if your firewall allows it.


Just wanted to let you know we released a new version of the Losant MQTT Python library with support for websockets!


Thanks Micahel, I was able to adjust the device config in my python script to use Websockets. I’m still, however getting an error when doing Device.Connect(). It’s giving me a socket.gaier

ror: [Errno -3] Name or Service not Known.

I don’t know if this is an issue of the way my raspberry pi is connecting to the proxy, or if there is something I need to change in the losant pyhton scripts.

I’ve attached a screen shot of the error i’m getting


Hmm “Name or service not known” seems like you are having local network issues with DNS resolution. Are you able to ping on the command line?


I am not, when trying to ping it says “name or service not known”


here is some background on the issue at hand.
I have connected my pi to the proxy by setting up the environment variables in /etc/environment
and /etc/apt/apt.conf.d/10proxy.

I am able to browse the web, I am also able to do apt-get update, and wget commands on the command line.
It just seems to be problem, like you said with dns resolution, because I am not able to ping to the broker or even

I am getting a different error now when connecting the device, it’s just giving me a connection timed out error.


I think that apt-get is using your proxy correctly (because you set up /etc/apt/apt.conf.d/10proxy) and wget is probably using the environment variables you set up. However, I just did a bit of research, and the underlying paho.mqtt.python does not seem to have any support for proxies (there is actually an open PR against the library for this feature.


You could run a local mosquitto broker to act as a bridge with it using websockets to connect, then you don’t need to use websockets locally, however you then also can’t use the Losant MQTT lib, as it doesn’t like talking to a bridge :wink:

You would have to build the mosquitto broker yourself with websocket support though.