# MQTT subscription using python and paho MQTT

**URL:** <https://forums.losant.com/t/mqtt-subscription-using-python-and-paho-mqtt/5608>\
**Category:** Help\
**Tags:** mqtt\
**Created:** [September 18, 2024, 7:39pm UTC](https://forums.losant.com/t/mqtt-subscription-using-python-and-paho-mqtt/5608 "2024-09-18T19:39:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Subrahmanyam\_Jonnala](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Subrahmanyam\_Jonnala](https://forums.losant.com/u/Subrahmanyam_Jonnala)\
**Post date:** [September 18, 2024, 7:39pm UTC](https://forums.losant.com/t/mqtt-subscription-using-python-and-paho-mqtt/5608/1 "2024-09-18T19:39:05Z")

</div>

Hi,

I am working to the following code to subscribe MQTT topic called “test” to receive flow stats from application workflow. The below code not returning any outputs.

```python
import paho.mqtt.client as mqtt

# MQTT broker information
mqtt_host = "mqtt://broker.losant.com"
mqtt_port = 1883
mqtt_client_id = "mqtt_66eaf9d2975adcda2edc253d"
username = "f7f1aaca-xxxx-xxxx-xxxx-0XXXXX"  
password = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"  

# Callback function for received messages
def on_message(client, userdata, msg):
    print(f"Received message on topic {msg.topic}: {str(msg.payload.decode('utf-8'))}")

def main():
    # Create MQTT client
    client = mqtt.Client(mqtt_client_id)
    client.on_message = on_message

    # Set username and password (if using Losant authentication)
    client.username_pw_set(username, password)

    # Connect to MQTT broker
    client.connect(mqtt_host, mqtt_port)

    # Subscribe to topics
    client.subscribe("test") # Subscribe to all topics (replace with specific topics if needed)

    # Start the MQTT loop
    client.loop_forever()

if __name__ == " __main__":
    main()

```

Subrahmanyam

---

<div class="post-metadata">

**Author:** ![Brandon\_Cannaday](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/brandon_cannaday/32/14_2.png) [@Brandon\_Cannaday](https://forums.losant.com/u/Brandon_Cannaday)\
**Post date:** [September 18, 2024, 7:43pm UTC](https://forums.losant.com/t/mqtt-subscription-using-python-and-paho-mqtt/5608/2 "2024-09-18T19:43:00Z")

</div>

To connect to Losant’s broker, the client ID must be a device ID. Your client ID `mqtt_66eaf9d2975adcda2edc253d` is not a device ID.

---

<div class="post-metadata">

**Author:** ![Subrahmanyam\_Jonnala](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Subrahmanyam\_Jonnala](https://forums.losant.com/u/Subrahmanyam_Jonnala)\
**Post date:** [September 18, 2024, 7:56pm UTC](https://forums.losant.com/t/mqtt-subscription-using-python-and-paho-mqtt/5608/3 "2024-09-18T19:56:56Z")

</div>

I tried with Device Id as well. Still, it didn’t work. Two quick questions

- I am publishing MQTT topic from application workflow. Do i still need to use to device id?
- Do i need mqtt:// in "mqtt\_host = “mqtt://broker.losant.com”. with mqtt, the code is not running.  
Sorry for the trouble, i am new to mqtt

---

<div class="post-metadata">

**Author:** ![Brandon\_Cannaday](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/brandon_cannaday/32/14_2.png) [@Brandon\_Cannaday](https://forums.losant.com/u/Brandon_Cannaday)\
**Post date:** [September 18, 2024, 8:07pm UTC](https://forums.losant.com/t/mqtt-subscription-using-python-and-paho-mqtt/5608/4 "2024-09-18T20:07:00Z")

</div>

> I am publishing MQTT topic from application workflow. Do i still need to use to device id?

The device ID is required to connect to the broker using any external client. This python script is an external client and therefore requires the client ID to be set to a device ID. Workflows can just publish directly to the broker - you can think of them as already connected to the broker behind the scenes.

> Do i need mqtt:// in "mqtt\_host = “mqtt://broker.losant.com”. with mqtt, the code is not running.

It’s not needed. You can see an example here:

[https://github.com/eclipse/paho.mqtt.python/blob/master/examples/client\_sub.py](https://github.com/eclipse/paho.mqtt.python/blob/master/examples/client_sub.py)

The other thing to check is that you’ve granted your access key and secret permission to subscribe to the `test` topic. By default, that is not allowed. Details can be found here:

[https://docs.losant.com/applications/access-keys/#additional-mqtt-topics-access](https://docs.losant.com/applications/access-keys/#additional-mqtt-topics-access)
