sbtinstruments / aiomqtt

The idiomatic asyncio MQTT client, wrapped around paho-mqtt
https://sbtinstruments.github.io/aiomqtt
BSD 3-Clause "New" or "Revised" License
392 stars 71 forks source link

[BUG] 127.0.0.1/localhost Work Incorrectly #294

Closed SnowCharmQ closed 2 months ago

SnowCharmQ commented 2 months ago

I use docker to deploy my MQTT application and MQTT broker EMQX on the same server. However, when I use 127.0.0.1/localhost as the hostname, the error occurred as follows: image The application can work correctly only when I set the hostname as the true IP like 192.168.77.55.

empicano commented 2 months ago

This is a bit difficult to debug without more details on the code. We have a script to run mosquitto locally in the repository. I can run that successfully together with something like the following:

import asyncio
import aiomqtt

async def main():
    async with aiomqtt.Client("localhost") as client:
        await client.publish("temperature/outside", payload=28.4)

asyncio.run(main())
SnowCharmQ commented 2 months ago

This is a bit difficult to debug without more details on the code. We have a script to run mosquitto locally in the repository. I can run that successfully together with something like the following:

import asyncio
import aiomqtt

async def main():
    async with aiomqtt.Client("localhost") as client:
        await client.publish("temperature/outside", payload=28.4)

asyncio.run(main())

I tested the script in my environment, it works as well. I have discovered the problem. I need to deploy the Python server by Docker, and I also use Docker to run an EMQX MQTT Broker. It seems that I can not directly access the broker in a Docker Python service by localhost or 127.0.0.1. Is there any solution to solve this problem instead of exposing the host IP to the Python service?

frederikaalund commented 2 months ago

Hi SnowCharmQ, thanks for opening this issue. :)

It does sound more like a Docker usage issue than an aiomqtt issue. Like Empicano already mentioned, we'll need to know a lot more about your setup to help you through. Even with more information, I doubt that our limited experience with Docker can help you. Maybe there is another forum with Docker experts that can guide you?

That being said, here are some general questions about your setup that might point you towards the root cause:

The list goes on. The common theme is: It's about your Docker config and not your aiomqtt client. :) In any case, I hope the above helps you along.