MQTT Web Socket in python

0 votes

So, I'm trying to connect with this code but this paho library does not work for it.

 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()

Is there support in python to subscribe to the MQTT broker with port 8080?

Apr 15, 2019 in IoT (Internet of Things) by Upasana
• 8,620 points
2,898 views

1 answer to this question.

0 votes

You can 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 run mosquito with the following

mosquitto -c /etc/mosquitto/mosquitto.conf

You should get this 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

answered Apr 15, 2019 by Shubham
• 13,490 points

Related Questions In IoT (Internet of Things)

0 votes
1 answer

Changing message format in KURA MQTT cloud client

The Eclipse Kura communicates with a MQTT ...READ MORE

answered Jul 29, 2018 in IoT (Internet of Things) by DataKing99
• 8,250 points
1,176 views
0 votes
1 answer

Publishing commands to device in IBM IoT using MQTT in Java

If you are publishing from an application, ...READ MORE

answered Jul 27, 2018 in IoT (Internet of Things) by anonymous2
• 4,240 points
1,510 views
0 votes
1 answer
0 votes
1 answer

The MQTT folder is missing in Bluemix IoT NodeJS code,

IoT-Workbench now uses new improved code generation ...READ MORE

answered Aug 8, 2018 in IoT (Internet of Things) by anonymous2
• 4,240 points
993 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,441 views
0 votes
1 answer
0 votes
1 answer

PWA : Using Bluetooth in my phone in Progressive Web Apps

Yes, you can access in Geo Location(for ...READ MORE

answered Dec 20, 2018 in IoT (Internet of Things) by Shubham
• 13,490 points
6,514 views
0 votes
1 answer

Paho MQTT : Exception in Thread

So, I'd reckon you try to take a ...READ MORE

answered Apr 4, 2019 in IoT (Internet of Things) by Shubham
• 13,490 points
1,496 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP