# Python mqtt over websockets

**URL:** <https://forums.losant.com/t/python-mqtt-over-websockets/1058>\
**Category:** Help\
**Created:** [August 8, 2018, 1:47pm UTC](https://forums.losant.com/t/python-mqtt-over-websockets/1058 "2018-08-08T13:47:43Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![robert\_choe](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@robert\_choe](https://forums.losant.com/u/robert_choe)\
**Post date:** [August 8, 2018, 1:47pm UTC](https://forums.losant.com/t/python-mqtt-over-websockets/1058/1 "2018-08-08T13:47:43Z")

</div>

Hi,  
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.

Thanks!  
Robert

---

<div class="post-metadata">

**Author:** ![Michael\_Kuehl](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/michael_kuehl/32/99_2.png) [@Michael\_Kuehl](https://forums.losant.com/u/Michael_Kuehl)\
**Post date:** [August 8, 2018, 6:29pm UTC](https://forums.losant.com/t/python-mqtt-over-websockets/1058/2 "2018-08-08T18:29:19Z")

</div>

The [Losant MQTT Python](https://github.com/Losant/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](http://www.eclipse.org/paho/clients/python/docs/#constructor-reinitialise) 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!

---

<div class="post-metadata">

**Author:** ![robert\_choe](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@robert\_choe](https://forums.losant.com/u/robert_choe)\
**Post date:** [August 8, 2018, 6:45pm UTC](https://forums.losant.com/t/python-mqtt-over-websockets/1058/3 "2018-08-08T18:45:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Michael\_Kuehl](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/michael_kuehl/32/99_2.png) [@Michael\_Kuehl](https://forums.losant.com/u/Michael_Kuehl)\
**Post date:** [August 8, 2018, 6:49pm UTC](https://forums.losant.com/t/python-mqtt-over-websockets/1058/4 "2018-08-08T18:49:55Z")

</div>

Yup! See the `transport` option [here](https://github.com/Losant/losant-mqtt-js#device). I would recommend using `wss` (secure websockets, over port 443) as opposed to `ws` (regular websockets, over port 80), if your firewall allows it.

---

<div class="post-metadata">

**Author:** ![Michael\_Kuehl](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/michael_kuehl/32/99_2.png) [@Michael\_Kuehl](https://forums.losant.com/u/Michael_Kuehl)\
**Post date:** [August 10, 2018, 3:38pm UTC](https://forums.losant.com/t/python-mqtt-over-websockets/1058/5 "2018-08-10T15:38:36Z")

</div>

Just wanted to let you know we released a new version of the [Losant MQTT Python](https://pypi.org/project/losant-mqtt/) library with support for websockets!

---

<div class="post-metadata">

**Author:** ![robert\_choe](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@robert\_choe](https://forums.losant.com/u/robert_choe)\
**Post date:** [August 10, 2018, 5:24pm UTC](https://forums.losant.com/t/python-mqtt-over-websockets/1058/6 "2018-08-10T17:24:54Z")

</div>

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

 ![IMG_0028](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/1X/1e3d3077830bad892b08f46bbf8bb793e9532dbd.jpeg) ![IMG_0028](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/1X/1e3d3077830bad892b08f46bbf8bb793e9532dbd.jpeg) 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

---

<div class="post-metadata">

**Author:** ![Michael\_Kuehl](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/michael_kuehl/32/99_2.png) [@Michael\_Kuehl](https://forums.losant.com/u/Michael_Kuehl)\
**Post date:** [August 10, 2018, 7:58pm UTC](https://forums.losant.com/t/python-mqtt-over-websockets/1058/7 "2018-08-10T19:58:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![robert\_choe](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@robert\_choe](https://forums.losant.com/u/robert_choe)\
**Post date:** [August 13, 2018, 12:29pm UTC](https://forums.losant.com/t/python-mqtt-over-websockets/1058/8 "2018-08-13T12:29:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![robert\_choe](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@robert\_choe](https://forums.losant.com/u/robert_choe)\
**Post date:** [August 13, 2018, 1:22pm UTC](https://forums.losant.com/t/python-mqtt-over-websockets/1058/9 "2018-08-13T13:22:37Z")

</div>

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 [google.com](http://google.com).

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

---

<div class="post-metadata">

**Author:** ![Michael\_Kuehl](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/michael_kuehl/32/99_2.png) [@Michael\_Kuehl](https://forums.losant.com/u/Michael_Kuehl)\
**Post date:** [August 14, 2018, 3:32pm UTC](https://forums.losant.com/t/python-mqtt-over-websockets/1058/10 "2018-08-14T15:32:39Z")

</div>

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](https://github.com/eclipse/paho.mqtt.python/) does not seem to have any support for proxies (there is actually an [open PR](https://github.com/eclipse/paho.mqtt.python/pull/315/files) against the library for this feature.

---

<div class="post-metadata">

**Author:** ![Tim\_Hoffman](https://avatars.discourse-cdn.com/v4/letter/t/ebca7d/32.png) [@Tim\_Hoffman](https://forums.losant.com/u/Tim_Hoffman)\
**Post date:** [August 17, 2018, 6:09am UTC](https://forums.losant.com/t/python-mqtt-over-websockets/1058/11 "2018-08-17T06:09:03Z")

</div>

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 😉

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