Intermediate 25 min

Step 1: Start the MQTT Broker

First, let’s get the MQTT broker running. We’ll use Mosquitto, a popular open-source MQTT broker, running in Docker.

Why Docker?

Docker makes it easy to run services without installing them directly on your system. You’ll have:

  • Isolated environment
  • Easy cleanup (just stop the container)
  • Consistent setup across different machines
  • No conflicts with other software

Create Docker Compose File

Create a file called docker-compose.yml in your project directory:


              version: '3.8'

services:
mosquitto:
  image: eclipse-mosquitto:2.0
  container_name: mqtt-broker
  ports:
    - "1883:1883"      # MQTT
    - "9001:9001"      # WebSockets (for Node-RED)
  volumes:
    - ./mosquitto/config:/mosquitto/config
    - ./mosquitto/data:/mosquitto/data
    - ./mosquitto/log:/mosquitto/log
  restart: unless-stopped
  networks:
    - iot-network

node-red:
  image: nodered/node-red:latest
  container_name: node-red
  ports:
    - "1880:1880"      # Node-RED UI
  volumes:
    - ./node-red/data:/data
  depends_on:
    - mosquitto
  restart: unless-stopped
  networks:
    - iot-network

networks:
iot-network:
  driver: bridge
            

Create Mosquitto Configuration

Create the directory structure and config file:

mkdir -p mosquitto/config mosquitto/data mosquitto/log

Create mosquitto/config/mosquitto.conf:


              # Basic Mosquitto configuration for development
# WARNING: This allows anonymous access - only for local development!

# Listener on port 1883 (standard MQTT)
listener 1883
protocol mqtt

# Allow anonymous connections (dev only!)
allow_anonymous true

# Persistence
persistence true
persistence_location /mosquitto/data/

# Logging
log_dest file /mosquitto/log/mosquitto.log
log_type all
log_timestamp true

# Connection settings
max_connections -1
max_inflight_messages 20
max_queued_messages 1000

# WebSockets for Node-RED
listener 9001
protocol websockets
            

Start the Services

Now start everything with Docker Compose:

docker-compose up -d

The -d flag runs containers in the background (detached mode).

Verify Broker is Running

Check if the broker is up:

docker-compose ps

You should see both mqtt-broker and node-red running.

Check the logs:

docker-compose logs mosquitto

You should see:

mosquitto 1.6.x running

Test Port Connectivity

Verify port 1883 is open:

# On macOS/Linux
nc -zv localhost 1883

# Or use telnet
telnet localhost 1883

If it connects, you’ll see something like:

Connection to localhost port 1883 [tcp/*] succeeded!

Quick Test with Mosquitto Client

If you have mosquitto_pub installed (comes with Mosquitto), test publishing:

mosquitto_pub -h localhost -p 1883 -t test/topic -m "Hello MQTT"

If you don’t have the client installed, we’ll test in the next step using Docker.

Checkpoint ✅

You should see:

  • ✅ Docker containers running (docker-compose ps)
  • ✅ Mosquitto logs showing broker started
  • ✅ Port 1883 accessible

If something’s wrong:

  • Check Docker is running: docker ps
  • Check ports aren’t in use: lsof -i :1883 (macOS/Linux)
  • Check logs: docker-compose logs mosquitto

Important Security Note

⚠️ This configuration allows anonymous access - it’s fine for local development, but never use this in production. In production, you’d add:

  • Username/password authentication
  • TLS/SSL encryption
  • Access control lists (ACLs)

We’ll cover production considerations in the last page.

What’s Next?

In the next page, you’ll test the broker manually using command-line tools. This helps you understand how MQTT publish/subscribe works before building the Python simulator.