Paho-mqtt client on Django [duplicate] - python

I am writing a django application which should act as MQTT publisher and as a subscriber.
Where should I start the paho client and run loop_forever() function.
Should it be in wsgi.py ?

Update:
If you need Django running in multiple threads then to publish messages from your Django app you can use helper functions from Publish module of Paho - https://eclipse.org/paho/clients/python/docs/#id17
You don't need to create an instance of mqtt client and start a loop in this case. And to subscribe to some topic consider running mqtt client as a standalone script and import there needed modules of your Django app (and don't forget to setup the Django environment in the script).
The answer below is good only if you run Django in a single thread, which is not usual in production.
Create mqtt.py in your application folder and put all related code there. For example:
import paho.mqtt.client as mqtt
def on_connect(client, userdata, rc):
client.subscribe("$SYS/#")
def on_message(client, userdata, msg):
# Do something
pass
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("iot.eclipse.org", 1883, 60)
Don't call loop_forever() here!
Then in your application __init__.py call loop_start():
from . import mqtt
mqtt.client.loop_start()
Using loop_start() instead of loop_forever() will give you not blocking background thread.

If you are using ASGI in your Django application you can use MQTTAsgi.
It's a complete protocol server for Django and MQTT.
Edit: Full disclosure I'm the author of MQTTAsgi.
Edit 2: To utilize the mqtt protocol server you can run your application, first you need to create a MQTT consumer:
from mqttasgi.consumers import MqttConsumer
class MyMqttConsumer(MqttConsumer):
async def connect(self):
await self.subscribe('my/testing/topic', 2)
async def receive(self, mqtt_message):
print('Received a message at topic:', mqtt_mesage['topic'])
print('With payload', mqtt_message['payload'])
print('And QOS:', mqtt_message['qos'])
pass
async def disconnect(self):
await self.unsubscribe('my/testing/topic')
Then you should add this protocol to the protocol router:
application = ProtocolTypeRouter({
'websocket': AllowedHostsOriginValidator(URLRouter([
url('.*', WebsocketConsumer)
])),
'mqtt': MyMqttConsumer,
....
})
Then you can run the mqtt protocol server with*:
mqttasgi -H localhost -p 1883 my_application.asgi:application
*Assuming the broker is in localhost and port 1883.
Thanks for the comments!

Related

Fastapi consuming live messages

I have a virtual assistant which receives messages and send it to a event broker (e.g rabbitmq).
An event broker allows me to connect my running assistant to other services that process the data coming in from conversations.
Example:
If I have a RabbitMQ server running, as well as another application that consumes the events, then this consumer needs to implement Pika's start_consuming() method with a callback action. Here's a simple example:
import json
import pika
def _callback(self, ch, method, properties, body):
# Do something useful with your incoming message body here, e.g.
# saving it to a database
print('Received event {}'.format(json.loads(body)))
if __name__ == '__main__':
# RabbitMQ credentials with username and password
credentials = pika.PlainCredentials('username', 'password')
# Pika connection to the RabbitMQ host - typically 'rabbit' in a
# docker environment, or 'localhost' in a local environment
connection = pika.BlockingConnection(
pika.ConnectionParameters('rabbit', credentials=credentials))
# start consumption of channel
channel = connection.channel()
channel.basic_consume(_callback,
queue='events',
no_ack=True)
channel.start_consuming()
What is the correct way to use fastapi with Pika to consume these live messages and saving it to a database ?
Do I need a websocket route ?

Not receiving messages with subscribe using Paho MQTT

I'm having a problem with subscribing to MQTT broker (using test.mosquitto.org with port 1883) with paho mqtt library. When I use mosquitto_sub client, I'm getting messages normally, but when I subscribe to the same topic in my Python script, callback never gets executed.
import paho.mqtt.client as mqtt
client_get = mqtt.Client(client_id='my_client', protocol=mqtt.MQTTv31)
client_get.connect('test.mosquitto.org', 1883)
client_get.loop_start()
def callback(client, userdata, message):
print(str(message.payload.decode("utf-8")))
client_get.on_message = callback
client_get.subscribe(topic, qos=1)
Try the following:
import paho.mqtt.client as mqtt
client_get = mqtt.Client(client_id='my_client', protocol=mqtt.MQTTv31)
client_get.connect('test.mosquitto.org', 1883)
def callback(client, userdata, message):
print(str(message.payload.decode("utf-8")))
client_get.on_message = callback
client_get.subscribe(topic, qos=1)
client_get.loop_forever()
I've moved the start_loop() to the end and changed it to loop_forever() which will block and keep the script running.

Python Paho MQTT: Broker not responding if flooded with mqtt messages?

I am Flask server enabled with Paho Mqtt with Python feature.
Various mqtt messages per second are send to the mqtt broker which are then stored in the database as soon as I am recieving messages but after some time mqtt broker fails to respond and slows down
Some messages are not recieved when inserting messages to database, as while inserting if some messages come, it doesn't catch.
3.My flask server is busy collecting mqtt messages, which slows down the website
here is the implementation part-
app.config['MQTT_BROKER_URL'] = 'xxxxxx'
app.config['MQTT_BROKER_PORT'] = 1883
app.config['MQTT_REFRESH_TIME'] = 1.0
mqtt = Mqtt(app)
socketio = SocketIO(app)
#socketio.on('publish')
def handle_publish(json_str):
data = json.loads(json_str)
mqtt.publish(data['topic'], data['message'])
#mqtt.on_connect()
def handle_connect(client, userdata, flags, rc):
mqtt.subscribe('topic1_name')
mqtt.subscribe('topic2_name')
#mqtt.on_message()
def handle_mqtt_message(client, userdata, message):
insert message to database
#app.route('/')
def index():
return render_template('new.html')
Is there anyway to seperate the script for collecting mqtt message and insertion to database to reduce the load? What could be done to make the mqtt broker more responsive?

MQTT over websocket in python

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.
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
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')
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)

Unable to receive more than 20 MQTT messages using Mosquitto/Paho for Python

I'm using the Mosquitto (now Paho) python MQTT client to connect to a HiveMQ broker. The code is very basic, as taken from their documentation here - https://pypi.python.org/pypi/paho-mqtt
#SUBSCRIBER
import paho.mqtt.client as mqtt
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, rc):
client.subscribe("GB/node0/", 2)
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print "Topic: ", msg.topic+'\nMessage: '+str(msg.payload)
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("192.168.15.4", 1883, 60)
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
client.loop_forever()
As you notice in client.subscribe(), the QoS is 2. This is the only modification to the official documentation's code.
The publishing client's code is-
#PUBLISHER
import paho.mqtt.client as mqtt
mqttc = mqtt.Client("python_pub")
mqttc.connect("192.168.15.4", 1883, 60)
mqttc.publish("GB/node0/", "Hello, World baby!",2)
Here also, the QoS is 2.
This modification of QoS results in only 20 messages being received by the subscriber. Upon further probing, I realized the problem is probably due to max_inflight_messages_set(), which is an option function which sets the maximum number of messages with QoS>0 that can be part way through their network flow at once. The default is 20.
However, changing it to any other value does not work. Also, why does the client think these messages are still inflight when they've been received? How do I resolve the problem? How do I ensure that the client understands these messages are not "inflight" and have been delivered?
Try calling mqttc.loop(2,10) after the mqttc.publish() in the publisher so the publisher can handle the QOS2 acknowledgement from the broker that it has received the publish.
The 2 second timeout and the 10 packets is probably more than is needed but it should work
#hardillb is right, you need some form of loop*() call. To be more certain, do this:
import paho.mqtt.client as mqtt
def on_publish(client, userdata, mid):
client.disconnect()
mqttc = mqtt.Client() # There's normally no need to set a client id.
mqttc.on_publish = on_publish
mqttc.connect("192.168.15.4", 1883, 60)
mqttc.publish("GB/node0/", "Hello, World baby!",2)
mqttc.loop_forever()
Or:
import paho.mqtt.publish as paho
paho.single("GB/node0/", "Hello, World baby!", qos=2, hostname="192.168.15.4")

Categories

Resources