This commit is contained in:
hubobel 2026-01-09 18:14:25 +01:00
parent b8d9ada053
commit ad820e6ec0

View file

@ -74,106 +74,7 @@ networks:
name: mqtt5-network
```
### 5.1 Public facing Mosquitto Websocket Server with Free SSL using Caddy Server
> [!NOTE]
> Setup and automate FREE valid SSL for Mosquitto Websocket (WSS), using [Caddy Server](https://caddyserver.com/) with very minimal effort.
<details>
<summary>Click here to expand for instructions</summary>
#### Setting up CaddyServer
Lets check the steps for setting it up
- Create folders for Caddy data and config
- Configure DNS with A record pointing to your MQTT public IP address
- Create a config file called 'Caddyfile'
- Create a combined docker-compose file with Caddy + Mosquitto
- Create containers using docker-compose run
#### Create folders for Caddy
```bash
# Caddy data & config files where certificates etc are stored
mkdir caddy_data
mkdir caddy_config
```
#### DNS Setup
```bash
# Create a DNS A/AAAA record pointing your domain to the public IP address
mqtt.domain.com A <public-IP-address-for-MQTT-instance>
```
Make sure to wait for the DNS record to complete propagation (depending on TTL). Otherwise automatic SSL creation would not work.
#### Caddyfile for configuration
Caddy uses 2 volumes for data (storing certificates etc) & config.
Create a file called 'Caddyfile' in the local folder for configuration, which will be mapped to /etc/caddy/Caddyfile through docker-compose file as below.
#### Content of configuration file called 'Caddyfile'
```bash
# Config file in the current folder
touch Caddyfile
```
_Add below content to `Caddyfile`_ created above.
```
mqtt.domain.com {
reverse_proxy ws://mqtt5:9001
}
```
#### Combined docker-compose.yml
```yaml
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
restart: unless-stopped
# caddy for HTTPS and reverse-proxy
caddy:
image: caddy:latest
container_name: caddy
restart: unless-stopped
ports:
- "80:80"
- "443:443"
- "443:443/udp"
volumes:
- ./Caddyfile:/etc/caddy/Caddyfile
- ./caddy_data:/data
- ./caddy_config:/config
# volumes for mapping data,config and log
volumes:
config:
data:
log:
caddy_data:
caddy_config:
networks:
default:
# Caddy and mosquitto should be in the same docker network
name: caddy-mqtt
```
```bash
# MQTT Connection URL would be
# WSS => Websocket Secure with SSL
wss://mqtt.domain.com:443
```
#### Using MQTTX Client
![alt text](mqttx-client.png)
</details>
## 6. Create and run docker container for MQTT
@ -230,65 +131,3 @@ Then restart the container
```bash
sudo docker restart <container-id>
```
## 8. Time to test !!!
### Install mosquitto client tools for testing
```bash
sudo apt install mosquitto-clients
```
### Let us start Subscriber now - topic name => 'hello/topic'
```bash
# 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
```
### Let us start Publising to that topic
```bash
# 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'
```
## You can find C/C++ code for mosquitto client
Check [main.cpp](main.cpp) for the mosquitto client code.
## You can also install a nice MQTT Web Client
Read more about it here => https://mqttx.app/
```bash
sudo docker run -d --name mqttx-web -p 80:80 emqx/mqttx-web
```
## Source/Reference for Mosquitto
Github => https://github.com/eclipse/mosquitto
##
![Static Badge](https://img.shields.io/badge/SPONSORING-red?style=for-the-badge)
If you use my projects or like them, consider sponsoring me. Anything helps and encourages me to keep going.
See details here: https://github.com/sponsors/sukesh-ak
Your sponsorship would help me not only maintain the projects I'm involved in, but also support my other community activities and purchase hardware for testing these libraries. If you're an individual user who has enjoyed my projects or benefited from my community work, please consider donating as a sign of appreciation. If you run a business that uses my work in your products, sponsoring my development makes good business sense: it ensures that the projects your product relies on stay healthy and actively maintained.
Thank you for considering supporting my work!