Skip to main content

Flask extension for the MQTT protocol

Project description


Flask Extension for the MQTT protocol. Basically it is a thin wrapper around paho-mqtt and aimes to simplify MQTT integration in Flask. MQTT is a machine-to-machine "Internet of Things" protocol and was designed for extremely lightweight publish/subscribe messaging transport.

Documentation Status PyPI version Travis CI

Find the documentation on


  • configuration via Flask config variables
  • auto-connect on start of your web application
  • publish and subscribe messages
  • use callbacks for certain topics
  • use one callback for all subscribed topics


Simply install the package as usual via pip:

$ pip install flask-mqtt


Basic Setup

from flask import Flask
from flask_mqtt import Mqtt

app = Flask(__name__)
app.config['MQTT_BROKER_URL'] = ''
app.config['MQTT_BROKER_PORT'] = 1883
app.config['MQTT_USERNAME'] = 'user'
app.config['MQTT_PASSWORD'] = 'secret'
app.config['MQTT_REFRESH_TIME'] = 1.0  # refresh time in seconds
mqtt = Mqtt(app)

def index():
    return render_template('index.html')

Subscribe to a topic

To subscribe to a topic simply use mqtt.subscribe(). To make sure the subscription gets handled correctly on startup place the subscription inside an on_connect() callback function.

def handle_connect(client, userdata, flags, rc):

To handle the subscribed messages you can define a handling function by decorating it with @mqtt.on_message().

def handle_mqtt_message(client, userdata, message):
    data = dict(

To unsubscribe do:


Or if you want to unsubscribe all topics use unsubscribe_all().



To publish a message you can use the publish() method.

mqtt.publish('home/mytopic', 'this is my message')

Small publish/subscribe MQTT client


A small Test application to show how to use Flask-MQTT.


import eventlet
import json
from flask import Flask, render_template
from flask_mqtt import Mqtt
from flask_socketio import SocketIO
from flask_bootstrap import Bootstrap


app = Flask(__name__)
app.config['SECRET'] = 'my secret key'
app.config['TEMPLATES_AUTO_RELOAD'] = True
app.config['MQTT_BROKER_URL'] = ''
app.config['MQTT_BROKER_PORT'] = 1883
app.config['MQTT_USERNAME'] = ''
app.config['MQTT_PASSWORD'] = ''
app.config['MQTT_KEEPALIVE'] = 5
app.config['MQTT_TLS_ENABLED'] = False

# Parameters for SSL enabled
# app.config['MQTT_BROKER_PORT'] = 8883
# app.config['MQTT_TLS_ENABLED'] = True
# app.config['MQTT_TLS_INSECURE'] = True
# app.config['MQTT_TLS_CA_CERTS'] = 'ca.crt'

mqtt = Mqtt(app)
socketio = SocketIO(app)
bootstrap = Bootstrap(app)

def index():
    return render_template('index.html')

def handle_publish(json_str):
    data = json.loads(json_str)
    mqtt.publish(data['topic'], data['message'])

def handle_subscribe(json_str):
    data = json.loads(json_str)

def handle_mqtt_message(client, userdata, message):
    data = dict(
    socketio.emit('mqtt_message', data=data)

def handle_logging(client, userdata, level, buf):
    print(level, buf)

if __name__ == '__main__':, host='', port=5000, use_reloader=True, debug=True)

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Flask-MQTT-1.0.3.tar.gz (7.0 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page