title: how to minimize the massive message delay paho mqtt
body: <p>I have seven raspberry pi's as clients a and one raspberry pi as client b which are transferring messages between each other via mqtt. It's working well so far, but there is a massive delay of over 2s sometimes. Most of the times the the delay is arround 0,02-0,2s but sometimes there are some delay-peaks with over 2s of delay.
  I think it appears as soon as client b gets too many messages at the same time from the a clients.
  Has anybody of you guys an idea what's the reason for this and how I can solve this?
  Thanks and stay healthy!</p>
  
  <p>When the run() is called, client a publishes to client b</p>
  
  <p>client a</p>
  
  <pre><code>from datetime import datetime
  import time
  import paho.mqtt.client as mqtt
  
  
  class CarClient:
  
      def __init__(self, id):
          self.client = mqtt.Client(id)
          self.client.connect("192.168.178.41", 1883, 60)  # connect to broker
          self.newSpeed = 0
          self.holdSpeedTime = 5
          self.speedDuration = 0
          self.id = id
          self.time_sent = 0
  
      def on_connect(self, mqttc, obj, flags, rc):
          print("connected" + str(rc))
  
      def on_message(self, mqttc, obj, msg):
          answerTuple = eval(msg.payload)
          self.newSpeed = int(answerTuple[0])
          self.holdSpeedTime = answerTuple[1]
          self.client.unsubscribe("test_channel")
          time_feedback = time.time()
          delay = time_feedback - self.time_sent
          print(str(delay))
  
      def on_disconnect(self, mqttc, flags, rc):
          print("disconnected" + str(rc))
  
      def on_publish(self, mqttc, obj, mid):
          print("Data published " + str(mid))
          print("published")
  
      def on_subscribe(self, mqttc, obj, mid, granted_qos):
          print("Subscribed: " + str(mid) + " " + str(granted_qos))
  
      def on_unsubscribe(self, mqttc, flags, rc):
          print("unsubscribed" + str(rc))
  
      def on_log(self, mqttc, obj, level, string):
          print(string)
  
      def run(self, carID, connectionPoint):
          self.time_sent = time.time()
          self.tuple = (carID, connectionPoint)
  
          # assining functions to callback
          self.client.on_disconnect = self.on_disconnect
          self.client.on_connect = self.on_connect
          self.client.on_message = self.on_message
          # self.client.on_subscribe = self.on_subscribe
          # self.client.on_unsubscribe = self.on_unsubscribe
          self.client.loop_start()
          self.client.subscribe(str(self.id), 0)  # subscribe topic
  
          self.client.publish("test_channel1", str(self.tuple))
  
          time.sleep(1)
  
          carFeedback = (self.newSpeed, self.holdSpeedTime)
          self.newSpeed = 0
          self.holdSpeedTime = 5
  
          return carFeedback
  </code></pre>
  
  <p>client b</p>
  
  <pre><code>import paho.mqtt.client as mqtt
  import time
  
  crossingList = []
  
  
  
  def on_subscribe(client, userdata, mid, granted_qos):  # create function for callback
      print("subscribed with qos", granted_qos, "\n")
  
  
  def on_disconnect(client, userdata, rc=0):
      print("disconnected")
  
  
  def on_connect(client, userdata, flags, rc):
      print("connected")
  
  
  def on_message(client, userdata, message):
      dataTuple = eval(message.payload)  # message is a tuple of (carID, connectionPoint)
      print(str(dataTuple))
      addToList(dataTuple)
  
  
  def on_publish(client, userdata, mid):
      print("published")
  
  
  def addToList(dataTuple):
      carID = dataTuple[0]
      connectionPoint = dataTuple[1]
      crossingList.append((carID, connectionPoint))
  
  
  def logic():
      carID = crossingList.__getitem__(0)[0]
      connectionPoint = crossingList.__getitem__(0)[1]
  
      if connectionPoint == 600:
  
          newSpeed = 400
          holdTime = 5
          client.publish( carID, str((newSpeed, holdTime)))
          del crossingList[0]
  
      else:
          newSpeed = 500
          holdTime = 3
          client.publish(carID, str((newSpeed, holdTime)))
          del crossingList[0]
  
  
  
  client = mqtt.Client("Logic")  # create client object
  
  client.on_subscribe = on_subscribe  # assign function to callback
  client.on_disconnect = on_disconnect  # assign function to callback
  client.on_connect = on_connect  # assign function to callback
  client.on_message = on_message
  client.connect("192.168.178.41", 1883, 60)  # establish connection
  time.sleep(0.1)
  client.loop_start()
  client.subscribe("test_channel1")
  count = 1
  while True:  # runs forever break with CTRL+C
      if len(crossingList) != 0:
          logic()
      else:
          count = count + 1
  </code></pre>