jzombie / docker-mqtt-mosquitto-cloudflare-tunnel

MQTT Broker via Cloudflare Tunnels, using Docker.
10 stars 0 forks source link

Clustering and bridging #1

Open jzombie opened 3 months ago

jzombie commented 3 months ago

To ensure that subscribers on one MQTT node receive messages published on another node, you can implement MQTT clustering or bridging. This way, messages are synchronized across multiple brokers, ensuring consistent delivery to all subscribers regardless of which broker they are connected to.

Using MQTT Bridging

MQTT bridging connects multiple MQTT brokers, allowing them to share messages. Below is an example configuration for bridging Mosquitto brokers.

Docker Compose Configuration

services:
  mosquitto1:
    image: eclipse-mosquitto:latest
    container_name: mosquitto1
    volumes:
      - ./mosquitto1.conf:/mosquitto/config/mosquitto.conf
    ports:
      - "1883:1883"
      - "9001:9001"
    restart: unless-stopped

  mosquitto2:
    image: eclipse-mosquitto:latest
    container_name: mosquitto2
    volumes:
      - ./mosquitto2.conf:/mosquitto/config/mosquitto.conf
    ports:
      - "1884:1883"
      - "9002:9001"
    restart: unless-stopped

  cloudflared:
    image: cloudflare/cloudflared:latest
    container_name: cloudflared
    command: tunnel --no-autoupdate run --token ${CLOUDFLARE_TUNNEL_TOKEN}
    restart: unless-stopped
    environment:
      - CLOUDFLARE_TUNNEL_TOKEN

Mosquitto Configuration for Bridging

Create configuration files for each Mosquitto broker to set up bridging.

mosquitto1.conf

persistence true
persistence_location /mosquitto/data/
log_dest file /mosquitto/log/mosquitto.log

listener 1883
allow_anonymous true

listener 9001
protocol websockets

# Bridge configuration to mosquitto2
connection bridge_to_mosquitto2
address mosquitto2:1883
topic # both 0

mosquitto2.conf

persistence true
persistence_location /mosquitto/data/
log_dest file /mosquitto/log/mosquitto.log

listener 1883
allow_anonymous true

listener 9002
protocol websockets

# Bridge configuration to mosquitto1
connection bridge_to_mosquitto1
address mosquitto1:1883
topic # both 0

Explanation

  1. Persistence: Ensures that messages are retained across broker restarts.
  2. Listener Ports: Configures the ports for MQTT and WebSocket listeners.
  3. Bridge Configuration: Sets up a bridge between mosquitto1 and mosquitto2. The topic line indicates that all topics (#) are shared between the brokers in both directions.

Step-by-Step Guide for Cloudflare Tunnel and Docker Compose

  1. Create and Configure Cloudflare Tunnel:

    • Log into Cloudflare Zero Trust.
    • Navigate to Tunnels under the Networks section.
    • Create a new tunnel, selecting Docker as your platform, and follow the provided instructions to run the connector on your machine.
  2. Update Docker Compose and Configuration Files:

    • Use the Docker Compose configuration above.
    • Create the Mosquitto configuration files (mosquitto1.conf and mosquitto2.conf) with the bridging setup.
    • Ensure your environment variables and tokens are properly configured in the .env file.
  3. Start Your Containers:

    • Run docker-compose up to start the Mosquitto brokers and Cloudflare tunnel.
  4. Verify Bridging:

    • Publish a message to a topic on mosquitto1 and subscribe to the same topic on mosquitto2 to ensure the message is received, verifying the bridge is working correctly.

By setting up MQTT bridging, you ensure that messages are propagated between brokers, allowing subscribers on different nodes to receive all relevant messages, thus maintaining consistency across your MQTT network.