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

Viewed 218

I am Flask server enabled with Paho Mqtt with Python feature.

  1. 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

  2. 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?

0 Answers
Related