Reputation: 1
I have a Docker Compose File, which creates multiple Containers. One container wih the mqtt broker:
mqtt:
image: eclipse-mosquitto
hostname: mosquitto
container_name: mqtt
restart: unless-stopped
ports:
- "1883:1883"
- "9001:9001"
volumes:
- ./mosquitto:/etc/mosquitto/
healthcheck:
test: ["CMD", "mosquitto_sub", "-t", "$$SYS/#", "-C", "1", "-i", "healthcheck", "-W", "3"]
interval: 10s
retries: 5
start_period: 30s
timeout: 10s
And one Python container which uses paho to connect to the Broker:
MQTT_SERVER = "host.docker.internal"
MQTT_PATH = "test_channel"
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
def on_message(client, userdata, msg):
print("Message received")
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(MQTT_SERVER, int(str(os.environ["MQTT_PORT"])), keepalive=60)
client.subscribe(MQTT_PATH)
client.loop_forever()
I then send with another python file from my PC (not in a docker Container) messages:
import json
import random
import time
import paho.mqtt.publish as publish
import datetime
MQTT_SERVER = "localhost"
MQTT_PATH = "test_channel"
while True:
data = {
"1" :1,
"2" : 2
}
message = {
"source": "source1",
"data": data,
"time": datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d %H:%M:%S.%f")
}
publish.single(MQTT_PATH, json.dumps(message), hostname=MQTT_SERVER)
time.sleep(0.1)
The messages get through and I can recieve them but it stops working after the keep_alive time. I guess the listener cant communicate with the broker.
I changed the keep_alive time and confirmed that this is the Problem.
Anyone knows how I can fix this?
Upvotes: 0
Views: 38