pi-webrtc

Self-hosted Mosquitto

這一頁還沒有中文版,以下顯示英文原文。

Run your own MQTT broker instead of a cloud service. Mosquitto is a small and popular MQTT broker for Linux.

pi-webrtc and the browser connect to the broker in different ways:

Who connectsProtocolPort in this guide
pi-webrtc on the deviceMQTT1883
The browserMQTT over WebSocket8083, or 443 through a reverse proxy

1. Install Mosquitto

On the server:

sudo apt update
sudo apt install mosquitto mosquitto-clients

2. Create a user

sudo mosquitto_passwd -c /etc/mosquitto/passwd <username>
sudo chown mosquitto:mosquitto /etc/mosquitto/passwd

The first command asks for a password.

3. Add the listeners

Create /etc/mosquitto/conf.d/pi-webrtc.conf:

# For pi-webrtc
listener 1883

# For browsers
listener 8083
protocol websockets

allow_anonymous false
password_file /etc/mosquitto/passwd

Restart Mosquitto:

sudo systemctl restart mosquitto

4. Test the broker

In one terminal, subscribe:

mosquitto_sub -h localhost -p 1883 -u <username> -P <password> -t 'test/#' -v

In a second terminal, publish:

mosquitto_pub -h localhost -p 1883 -u <username> -P <password> -t test/hello -m hi

The first terminal should print test/hello hi.

5. Add TLS for browsers

The web app uses HTTPS, so the browser can only use a secure WebSocket (wss://). Put the broker behind a reverse proxy that has a TLS certificate. For example, with nginx:

location /mqtt {
    proxy_pass http://127.0.0.1:8083;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

The browser then connects to port 443 with the path /mqtt.

6. Connect pi-webrtc

./pi-webrtc ... \
    --use-mqtt \
    --mqtt-host=<broker-host> \
    --mqtt-port=1883 \
    --mqtt-username=<username> \
    --mqtt-password=<password>

If the device connects to the broker over the internet, use TLS on port 8883 instead of 1883. See the Mosquitto TLS guide.

The client library

pi-webrtc uses the Mosquitto client library from your OS. The release needs libmosquitto1, and building from source needs libmosquitto-dev. Both come from apt, so you do not need to build Mosquitto yourself.

在 GitHub 上編輯此頁

本頁內容