How to integrate MQTT in Django Projects?

Deepak Radhakrishnan


Hello folks , welcome back to Techrover . Have you ever tried making an IoT application for controlling your smart devices ? . Its so crazy to make your own application that controls your smart devices.



Today we gonna learn how easily we can attach an MQTT service with our Django application.

I hosted an MQTT service on my server and publish some data over a channel. We are going to setup an MQTT client in our Django application and spin-up the MQTT as service to listen the channel.


  • A running Django application
  • Hosted or locally running MQTT server


To get it working we need to install an MQTT client along with our Django application packages . I have chosen ‘paho mqtt client’ because its more compatible with python. To install paho mqtt client ,

pip install paho-mqtt

Always remember to use a virtual environment when installing packages , as I always say.


To write our mqtt logic we need to create a file called ‘’ . Inside this we will be writing our program logic , subscriptions and listeners. For example,

import paho.mqtt.client as mqtt
import json

def on_connect(client, userdata, flags, rc):
    if rc==0:
        client.connected_flag=True #set flag
        print("MQTT connected OK")
        print("Bad connection Returned code=",rc)

def on_message(client, userdata, msg):
    topic = msg.topic
    print("message received")

    #msg.append(m)#put messages in list
    #q.put(m) #put messages on queue
    #Do something .....

mqtt.Client.connected_flag=False#create flag in class
broker="" #replace with your mqtt server ip
client = mqtt.Client("server") #create new instance 
client.on_connect=on_connect  #bind call back function
client.on_message = on_message #bind an action method 
client.username_pw_set(username="your mqtt username",password="your mqtt password") # replace with your credentials 
print("Connecting to broker ",broker)
client.connect(broker)  #connect to the broker 

Now all we have to do is to create a loop that listen to the activities around the mqtt server which we have configured . We cannot create a while loop to do the task . The best practice is to do this is to write it in the file . Instead of using loop_forever() we are using loop_start() , so that it will not effect the main background thread.

from . import mqtt



I have subscribed a topic called ‘ping’ in our file so that it can listen to the topic ping . I am using my command prompt to publish data to the mqtt server. Every clients subscribed to that topic will get notified when I publish data. Let’s see,

The left screen is belongs to our Django application console and the right one is command prompt. We can observe that the message is received at our end .

This can be used to control and monitoring your smart devices , your home security application baby monitoring or anything .


What I have written is just a basic block of codes to get connected with your mqtt broker with a username and password . You can make it more secure by setting up TLS ports tokens and many more things .

I hope you got a basic idea of integrating MQTT client with our Django application. Do comment if you have any doubts or need any assistance for building you application . Please do share the article so that it will encourage me to write more posts.

How useful was this post?

Click on a starts to rate it!

Average rating / 5. Vote count:

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow me on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

%d bloggers like this: