8

is there any support in python to subscribe on mqtt broker with port 8080

 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()   
 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("test/iot", 0)

 mqttc.loop_forever()

i can not connect with this code. Mosquitto has websocket support at port 8080 but this paho library does not work for it. any solution for python? i am using python 2.7 on windows 10.

minghua
  • 5,981
  • 6
  • 45
  • 71
Smit Gardhariya
  • 97
  • 1
  • 3
  • 8
  • I think websocket is its own protocol, not same as MQ, that is (or uses) a different protocol, it's like trying to compare apples and tomatoes, it doesn't work except that they are both roundish (i.e. both are IP-based protocols). Try port 1883 which is unencrypted MQTT. – DisappointedByUnaccountableMod Feb 25 '16 at 09:21
  • I tried and it worked on 8080 but i want to do with 8080 as mosquitto broker has support of websocket on 8080 but this library does not work for 8080. – Smit Gardhariya Feb 26 '16 at 13:52
  • What benefit do you think there is to running a native client over Websockets? – hardillb Feb 26 '16 at 15:10
  • dear hardlib i made an IOT application in android and iOS that can send MQTT message only on websocket port 8080 so i need to connect my client device on port 8080.... javascript has support of mqtt on websocket library but python dsnt have it... – Smit Gardhariya Feb 28 '16 at 02:35
  • You have a client app that can only talk MQTT over websocket on 8080. So does that mean your question should be 'Is there a python lib which provides MQTT server over websocket (on 8080)? – DisappointedByUnaccountableMod Feb 29 '16 at 13:13
  • I want to know whether any library that can subscribe to MQTT on websocket in python is available not or any way to do it? I am using broker named test.mosquito.org which is MQTT broker – Smit Gardhariya Mar 01 '16 at 15:25
  • You shouldn't be using test.mosquito.org for anything other than a little light testing. But it (and pretty much any other broker) is listening on 1883 for native MQTT – hardillb Mar 20 '16 at 17:15

3 Answers3

13

The Paho MQTT module introduced websocket support some days ago. I don't think it is released yet, but you can install from the master under Linux branch using

pip install git+git://github.com/eclipse/paho.mqtt.python.git

Also works under windows. (Thanks for info from the comments)

You can use the websockets as transport by connecting with

mqttc = mqtt.Client(transport="websockets")

UPDATE:

If you try to use the websocket protocol with the python client because you also need to connect a browser client (for example MQTT.js) then you can also configure mosquitto to listen to websockets and the normal mqtt protocol.

Simply create a configuration file for example in

/etc/mosquitto/mosquitto.conf

with the following contents:

listener 1883
protocol mqtt

listener 9001
protocol websockets

Then you can then run mosquitto with

mosquitto -c /etc/mosquitto/mosquitto.conf

You should see similar output:

1469015320: mosquitto version 1.4.8 (build date 2016-05-3112:07:40+0200) starting
1469015320: Config loaded from /etc/mosquitto/mosquitto1.conf.
1469015320: Opening ipv4 listen socket on port 1883.
1469015320: Opening ipv6 listen socket on port 1883.
1469015320: Opening websockets listen socket on port 9001.

Your python client then connects to port 1883 and the browser client to 9001

You can use what-mqtt browser client to test the websocket listener. Just point it to ws://localhost:9001

Fl0v0
  • 900
  • 11
  • 27
  • Still not getting my messages..... this code let me connect to websocket but I m not getting messages. – Smit Gardhariya Jun 18 '16 at 10:11
  • @SmitGardhariya After reading the comments on your questions, it seems to me, that you want to use websockets because another client needs to connect to mosquitto using websockets. What i found out, was that mosquitto can listen to websockets AND the normal mqtt connections. You can add an extra listener in the config file: `listener 9001 protocol websockets` – Fl0v0 Jul 06 '16 at 08:30
  • @FI0v0 I got the solution with above suggestions . but can you tell me where the config file stored for python library named "paho" I also would like to try your suggestion. Thank You buddy .... :) – Smit Gardhariya Jul 20 '16 at 09:28
  • @SmitGardhariya I added an example how to configure the mosquitto broker. I don't understand what you mean with: but can you tell me 'where the config file stored for python library named "paho"'. You are using mosquitto as a broker right? – Fl0v0 Jul 20 '16 at 12:02
  • @FI0v0 yes i m using tht one – Smit Gardhariya Aug 22 '16 at 08:27
  • @SmitGardhariya Ok, what also is important is that you have mosquitto compiled with websocket support. Have you managed to start the broker with your configuration? – Fl0v0 Aug 22 '16 at 09:36
  • @FI0v0 ... ya i have started the broker with my configuration ... i am running it on websocket support given by "paho" in python and agular JS – Smit Gardhariya Oct 14 '16 at 05:48
  • @SmitGardhariya So it works? You should be able to connect to mosquitto with the python client on port 1883 without websocket and from the browser with the js client on port 9001 via websocket – Fl0v0 Oct 14 '16 at 07:23
  • No u didn't get me ..... I m doing communication on port 8080 instead of 1883...... So my mobile app can communicate to device directly – Smit Gardhariya Oct 14 '16 at 08:45
6

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')
minghua
  • 5,981
  • 6
  • 45
  • 71
  • by the way the paho code implemented websocket protocol by itself. you don't even need to install a websocket package. – minghua Jun 16 '16 at 06:04
  • the june 3 commit is one after the tag v1.2. that hints the feature was added or fixed by v1.2. the previous v1.1 was tagged on jan 29 of 2016. – minghua Jun 16 '16 at 13:59
  • Thnx minghua .... I tried it but I still not getting my messages..... this code let me connect to websocket but I m not getting messages. – Smit Gardhariya Jun 18 '16 at 10:10
  • wonder the environment you run in. cygwin? just tried it again, it works for me. put the file into the paho checkout under `paho.mqtt.python/src` and run from within that directory. guess subscribing to # may be problematic on some platforms. – minghua Jun 19 '16 at 07:23
  • 1
    thnx minghua it is working on windows too.... jst need to add transport=websocket – Smit Gardhariya Jun 21 '16 at 11:51
  • thanks smit for sharing the good news about windows. maybe it's just my mix of windows/python/cygwin having a problem. – minghua Jun 22 '16 at 18:37
2

No, the Python version of the Paho library does not support MQTT over Websockets.

In fact I believe the only native client side (not in a browser) MQTT library that supports MQTT over Websockets is the NodeJS module (https://www.npmjs.com/package/mqtt)

hardillb
  • 54,545
  • 11
  • 67
  • 105
  • i already have done it on Nodejs with library named "mows" that is mqtt on websocket but i want to do it in python so any idea do you have to implement it on python? – Smit Gardhariya Feb 26 '16 at 13:53
  • Have you tried goggling for _python mqtt websocket_? – DisappointedByUnaccountableMod Feb 26 '16 at 14:28
  • yes barny , i have done it but i cant find anything useful in google... if you have solution then tell me – Smit Gardhariya Feb 27 '16 at 08:12
  • @SmitGardhariya I used `hbmqtt` python library in one of my work projects. It had a great support and active contributions. I found some limitations though: 1. Hbmqtt won't support proxy as it uses `websockets 7` library for sock connections under the hood which haven't had a full support for proxies. 2. Message sent is not using a asyncio.StreamReader or Writer which makes the library to silently crash when you run on slow networks[ You can achieve this by sending pack in chunks of smaller bits than bandwidth]. – bh4r4th Jun 03 '19 at 07:58
  • @SmitGardhariya HBMQTT does support ws, wss, mqtt, mqtts, wss with the username and password authentication. Most importantly Last_will_and_testament. QOS_1 and QOS_2 acks are bit tricky to handle sometimes; if dealing with continuous message streams. – bh4r4th Jun 03 '19 at 08:00
  • This is not true for 2022. Here is an example https://github.com/eclipse/paho.mqtt.python/blob/master/examples/client_sub-ws.py – programandoconro May 13 '22 at 01:48