localhost:1883 — MQTT
MQTT (Message Queuing Telemetry Transport) is the standard messaging protocol for IoT and home automation. It runs on a publish-subscribe model: devices (clients) connect to a central broker and publish messages to topics, while other clients subscribe to receive them. Port 1883 is the unencrypted default. Port 8883 is the TLS-encrypted version.
If you run Home Assistant, Node-RED, OpenHAB, Zigbee2MQTT, Tasmota, or any IoT project with more than a few devices, you will almost certainly set up an MQTT broker — most commonly Mosquitto.
Install Mosquitto (Most Common MQTT Broker)
# Ubuntu/Debian
sudo apt install mosquitto mosquitto-clients
# Start and enable
sudo systemctl enable --now mosquitto
# Default config file
sudo nano /etc/mosquitto/mosquitto.conf
# Verify it's listening
sudo ss -tlnp | grep 1883
Quick Test — Publish and Subscribe
# Terminal 1: Subscribe to a topic
mosquitto_sub -h localhost -t "test/topic"
# Terminal 2: Publish a message
mosquitto_pub -h localhost -t "test/topic" -m "hello from MQTT"
# Terminal 1 will display: hello from MQTT
Connection Strings by Language
# Python (paho-mqtt)
import paho.mqtt.client as mqtt
client = mqtt.Client()
client.connect("localhost", 1883, 60)
client.publish("home/temp", "22.5")
# Subscribe
def on_message(client, userdata, msg):
print(f"{msg.topic}: {msg.payload.decode()}")
client.on_message = on_message
client.subscribe("home/#")
client.loop_forever()
# Node.js (mqtt package)
const mqtt = require('mqtt')
const client = mqtt.connect('mqtt://localhost:1883')
client.on('connect', () => {
client.subscribe('home/#')
client.publish('home/temp', '22.5')
})
client.on('message', (topic, message) => {
console.log(topic, message.toString())
})
Mosquitto Configuration — Add Authentication
By default, Mosquitto on new installations rejects anonymous connections in newer versions. Create a password file:
# Create user
sudo mosquitto_passwd -c /etc/mosquitto/passwd myuser
# Add to /etc/mosquitto/mosquitto.conf:
allow_anonymous false
password_file /etc/mosquitto/passwd
listener 1883 127.0.0.1 # Bind to localhost only
# Restart
sudo systemctl restart mosquitto
MQTT Topics — Best Practices
| Topic Pattern | Example | Use Case |
|---|---|---|
| area/device/property | living-room/lamp/state | Smart home device states |
| area/device/set | living-room/lamp/set | Commands sent to devices |
| sensors/location/type | sensors/garden/temperature | Environmental sensors |
| $SYS/# | $SYS/broker/clients/total | Mosquitto broker statistics |
The # wildcard matches everything below a level. + matches a single level. home/+/temperature matches home/kitchen/temperature and home/bedroom/temperature.
Home Assistant MQTT Integration
Home Assistant has built-in MQTT support. Add Mosquitto via the Add-on Store (if running Home Assistant OS) or configure via Settings → Devices & Services → Add Integration → MQTT. Point it to localhost port 1883 with your Mosquitto credentials.
Troubleshooting
Connection refused on 1883: Mosquitto is not running (sudo systemctl status mosquitto) or is bound to 127.0.0.1 only. If connecting from another device on the network, change the listener in mosquitto.conf from 127.0.0.1 to 0.0.0.0 and restart.
Not authorized: Anonymous connections are disabled. Pass username and password: mosquitto_pub -u myuser -P mypassword -h localhost -t "test" -m "hello"
Messages not arriving: Verify the subscribe topic exactly matches the publish topic. MQTT topics are case-sensitive. Use mosquitto_sub -t "#" -v to subscribe to all topics and debug what is actually being published.