- install mosquitto
apt-get install mosquitto mosquitto-clients
- add config to create server
echo "allow_anonymous true" > /etc/mosquitto/conf.d/nework.conf
echo "listener 1883 0.0.0.0" >> /etc/mosquitto/conf.d/nework.conf
- restart mosquito daemon
/etc/init.d/mosquitto restart
- mosquitto subscribe: obtain messages
mosquitto_sub -t test --insecure
- mosquitto publish: send messages to subscriptors
mosquitto_pub -h localhost -t test -m "Hello, MQTT!"
- get my ip, in my case: 10.217.252.10
ip addr show
- create folder
mkdir -p /home/projects/pdm-python
cd /home/projects/pdm-python
- create script
nano index.py
import paho.mqtt.client as mqtt
print("Mosquito client:")
def on_connect(client, userdata, flags, rc):
print(f"Connected with result code {rc}")
client.subscribe("test")
def on_message(client, userdata, msg):
print(f"{msg.topic}: {msg.payload.decode()}")
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("10.217.252.83", 1883, 60)
client.loop_forever()
- build image
podman build pdm-python .
- create network
podman network create localdomain
- run image
podman run --network=localdomain -ti pdm-python
- send message to mosquito server and check in the podman container
mosquitto_pub -h localhost -t test -m "Hello, MQTT!"
- podman container response:
test: Hello, MQTT!