MQTT Mosquitto
hero_image
link
manufacture_description
2. Create base folder for mqtt configuration
mkdir mqtt5
cd mqtt5
# for storing mosquitto.conf and pwfile (for password)
mkdir config
link
3. Create Mosquitto config file - mosquitto.conf
nano config/mosquitto.conf
Basic configuration file content below including websocket config
allow_anonymous false
listener 1883
listener 9001
protocol websockets
persistence true
password_file /mosquitto/config/pwfile
persistence_file mosquitto.db
persistence_location /mosquitto/data/
link
4. Create Mosquitto password file - pwfile
touch config/pwfile
link
5. Create docker-compose file called 'docker-compose.yml'
version: "3.7"
services:
# mqtt5 eclipse-mosquitto
mqtt5:
image: eclipse-mosquitto
container_name: mqtt5
ports:
- "1883:1883" #default mqtt port
- "9001:9001" #default mqtt port for websockets
volumes:
- ./config:/mosquitto/config:rw
- ./data:/mosquitto/data:rw
- ./log:/mosquitto/log:rw
# volumes for mapping data,config and log
volumes:
config:
data:
log:
networks:
default:
name: mqtt5-network
link
6. Create and run docker container for MQTT
# In case you don't have docker-compose you can install it
sudo apt install docker-compose
# Run the docker container for mqtt
sudo docker-compose -p mqtt5 up -d
link
Check if the container is up and working (note down container-id)
sudo docker ps
link
7. Create a user/password in the pwfile
# login interactively into the mqtt container
sudo docker exec -it <container-id> sh
# add user and it will prompt for password
mosquitto_passwd -c /mosquitto/config/pwfile user1
# delete user command format
mosquitto_passwd -D /mosquitto/config/pwfile <user-name-to-delete>
# type 'exit' to exit out of docker container prompt
Then restart the container
sudo docker restart <container-id>
link
8. Time to test !!!
link
Install mosquitto client tools for testing
sudo apt install mosquitto-clients
link
Let us start Subscriber now - topic name => 'hello/topic'
# Without authentication
mosquitto_sub -v -t 'hello/topic'
# With authentication
mosquitto_sub -v -t 'hello/topic' -u user1 -P <password>
# Alternate way in url format
# Format => mqtt(s)://[username[:password]@]host[:port]/topic
mosquitto_sub -v -L mqtt://user1:abc123@localhost/test/topic
link
Let us start Publising to that topic
# Without authentication
mosquitto_pub -t 'hello/topic' -m 'hello MQTT'
# With authentication
mosquitto_pub -t 'hello/topic' -m 'hello MQTT' -u user1 -P <password>
# Alternate way in url format
# Format => mqtt(s)://[username[:password]@]host[:port]/topic
mosquitto_pub -L mqtt://user1:abc123@localhost/test/topic -m 'hello MQTT'
link
You can also install a nice MQTT Web Client
Read more about it here => https://mqttx.app/
sudo docker run -d --name mqttx-web -p 80:80 emqx/mqttx-web
link
Source/Reference for Mosquitto
Github => https://github.com/eclipse/mosquitto
don't use
localhost
. Use IP address assigend by router i.e. 192.168.0.111