Skip to main content
MQTT Gateway connects to an MQTT broker to receive messages. Broker configuration is stored in the database and can be updated without restarting the service.

Broker connection settings

MQTT broker configurations are stored in the mqtt_servers table in the database. The gateway automatically selects the enabled broker with the highest priority.

Server selection logic

When the gateway starts, it loads the MQTT server using this query:
The gateway prioritizes servers where is_default=True, then by lowest id.

Server parameters

string
required
MQTT broker hostname or IP address.Example: 192.168.0.137
integer
default:"1883"
MQTT broker port number.Common ports:
  • 1883 - Standard MQTT
  • 8883 - MQTT over TLS
Example: 1883
string
Optional username for MQTT broker authentication.Leave empty if the broker doesn’t require authentication.
string
Optional password for MQTT broker authentication.Leave empty if the broker doesn’t require authentication.
boolean
default:"true"
Whether this MQTT server configuration is active.Set to false to disable without deleting the configuration.
boolean
default:"true"
Whether this is the default MQTT server.When multiple enabled servers exist, the gateway uses the one marked as default.

Authentication

If your MQTT broker requires authentication, set both username and password fields:
Both fields must be set for authentication to be used. If either is missing, the gateway connects without credentials.

Connection parameters

The MQTT client connection is configured using environment variables:
string
default:"mqtt-gateway"
Unique client identifier for the MQTT connection.See environment variables for details.
integer
default:"60"
Keepalive interval in seconds for maintaining the MQTT connection.See environment variables for details.

Connection establishment

The gateway establishes the MQTT connection at startup:
The connection runs in a background thread, allowing the main thread to handle flow reloading.

Topic subscriptions

The gateway automatically subscribes to MQTT topics based on enabled flows in the database.

Initial subscription

When the connection is established, the gateway subscribes to all topics from enabled flows:

Dynamic subscription updates

The gateway periodically reloads flows from the database and updates subscriptions:
This allows you to add or remove topic subscriptions by updating flows in the database without restarting the gateway.
The reload interval is controlled by the FLOWS_RELOAD_INTERVAL_SECONDS environment variable (default: 600 seconds).

Topic matching

The gateway supports MQTT wildcard patterns in topic subscriptions:
  • + - Single-level wildcard (matches one topic level)
  • # - Multi-level wildcard (matches zero or more topic levels)
Example topics:
  • sensors/temperature - Exact match
  • sensors/+/temperature - Matches sensors/kitchen/temperature, sensors/bedroom/temperature
  • sensors/# - Matches all topics under sensors/
When a message arrives, the gateway matches it against all flow topics:

Message processing

Incoming messages must be valid JSON objects:
Messages with invalid JSON or non-object payloads are logged and discarded. Ensure your MQTT publishers send valid JSON objects.

Connection error handling

If the connection fails, the error is logged but the gateway continues running:
The Paho MQTT client library automatically attempts to reconnect using its built-in reconnection logic.