title: Why does MQTT not connect to the server?
body: <p>My purpose is to achieve MQTT message publish upon postsave signal sent my DJango models. I have registered on_connect, on disconnect, on_publish callbacks to check when the relevant process is executed. However, none of callbacks I registered works. Here is my post_save signal code.
  Can someone please give me a hint where I do mistake?</p>
  <pre><code>import paho.mqtt.client as mqtt
  
  
  def on_connect(client, userdata, flags, rc):
      print(&quot;connecting&quot;)
      print(&quot;client:&quot;+str(client))
      print(&quot;userdata:&quot;+str(userdata))
      print(&quot;flags:&quot;+str(flags))
      print(&quot;result:&quot;+str(rc))
      global flag_connected
      flag_connected=1
  
  
  def on_publish(client, userdata, mid):
      print(&quot;message published&quot;)
  
  
  def on_connect_fail(client, userdata, flags, rc):
      print(&quot;failed to connect&quot;)
      print(&quot;client:&quot;+str(client))
      print(&quot;userdata:&quot;+str(userdata))
      print(&quot;flags:&quot;+str(flags))
      print(&quot;result:&quot;+str(rc))
      global flag_connected
      flag_connected=0
  
  
  def on_disconnect(client, userdata, rc):
     global flag_connected
     flag_connected = 0
  
  
  
    
  
  def notify_windowsApp(sender,instance, **kwargs):
      
  
      new_truck_registered={
          &quot;identity&quot;:str(instance.truck_identity),
          &quot;doorName&quot;:str(instance.registered_door.door_name),
          &quot;doorNumber&quot;:str(instance.registered_door.door_number),
          &quot;entryTag&quot;:str(instance.entry_tag),
         
          #&quot;depatureTag&quot;:str(instance.departure_tag), 
          &quot;entrytime&quot;:instance.entry_time.strftime('%Y-%m-%d::%H-%M'),
  
      }
  
  
  
      
  
  
  
      sendToClient(new_truck_registered)
  
  
  def sendToClient(payloadTobeSent):
      
  
      client=mqtt.Client(client_id=&quot;django_post_save_signal&quot;,
      clean_session=True, )
      mqttBroker=&quot;broker.hivemq.com&quot;
      client.on_connect=on_connect
      client.on_publish=on_publish
      client.on_connect_fail=on_connect_fail
      client.on_disconnect=on_disconnect
      client.connect(mqttBroker)
      client.publish('baydoor/truckentrance',payload=str(payloadTobeSent),qos=1, retain=True)
  
  </code></pre>