title: paho-mqtt subscribe check subscription status
body: <p>I am using <a href="https://pypi.python.org/pypi/paho-mqtt/1.2" rel="nofollow noreferrer">paho-mqtt 1.2</a> in python2.7 to listen to messages broadcasted by a broker, after subscribing to specific topics.</p>
  
  <p>It's initialized like</p>
  
  <pre><code>import paho.mqtt.client as mqtt    #python mqtt package
  
  # initialize MQTT client
  mqttc = mqtt.Client()
  
  # register listener functions    
  mqttc.on_connect   = on_connect
  mqttc.on_subscribe = on_subscribe
  mqttc.on_message   = on_message
  
  # connect to MQTT broker
  mqttc.connect(&lt;IP&gt;, &lt;PORT&gt;, &lt;KEEPALIVE_INTERVAL&gt;)
  
  # subscribe a topic to the broker
  subscr = mqttc.subscribe(&lt;TOPIC_NAME&gt;, 0)
  </code></pre>
  
  <p>with the custom functions</p>
  
  <pre><code>def on_connect(client, userdata, flags, rc):
      print "connected"
  
  def on_subscribe(client, userdata, mid, granted_qos):
      print "subscribed"
  
  def on_message(client, userdata, msg):
      print "message received"
  </code></pre>
  
  <p>From the API I can read about the subscribe() function</p>
  
  <p><em>The function returns a tuple (result, mid), where result is
  MQTT_ERR_SUCCESS to indicate success or (MQTT_ERR_NO_CONN, None) if the
  client is not currently connected.  mid is the message ID for the
  subscribe request. The mid value can be used to track the subscribe
  request by checking against the mid argument in the on_subscribe()
  callback if it is defined.</em></p>
  
  <p>I am now trying to retrieve a status about the subscription, i.e. if I subscribed a VALID topic. In my understanding, the subscribe() function only returns an error code, if the topic has an invalid format. But I want to extract information, if I subscribed to a topic which is ACTUALLY broadcasted.</p>
  
  <p>So far I get an on_subscribe() call including a subscription ID, no matter which topic (valid or invalid) I subscribe.</p>