MQTT¶
MQTT is a lightweight messaging protocol for small sensors and mobile devices, ideal for IoT and low-bandwidth environments. Upstream docs: https://mqtt.org/
Features¶
- QoS Levels: Support for Quality of Service levels 0 (at most once), 1 (at least once), and 2 (exactly once)
- Retained Messages: Messages can be retained by the broker for new subscribers
- TLS/SSL Support: Secure connections via the
mqtts://scheme - Authentication: Username and password, username only, or password only (such as an access token), as MQTT v5 allows
- Clean Session: Control whether the broker maintains session state
- Managed Connection: The first send connects to the broker, later sends reuse the connection, and an idle connection is closed after 60 seconds
Getting Started¶
MQTT is widely used in IoT, home automation, and real-time messaging applications. The protocol is supported by many popular brokers:
- Mosquitto: A popular open-source MQTT broker
- Home Assistant: Built-in MQTT support for smart home automation
- EMQX: Enterprise-grade MQTT broker
- HiveMQ: Scalable MQTT platform
MQTT 5
Shoutrrr connects with MQTT 5, so the broker must support it. A broker that only speaks MQTT 3.1.1 refuses the connection. Each send then waits until its time runs out and fails, and Shoutrrr logs the broker's refusal as a warning.
To send notifications via MQTT, you need a running MQTT broker and a topic to publish to.
Topics use a hierarchical naming scheme with forward slashes (e.g., home/alerts, sensors/temperature).
A URL without a host, such as mqtt:///home/alerts, connects to a broker on localhost.
The topic is the URL path after its first slash, so a topic that starts with a slash, such as /home/alerts, which MQTT treats as distinct from home/alerts, is written with two: mqtt://broker.example.com//home/alerts.
A topic name cannot contain the + or # wildcards, which MQTT only allows in subscriptions, so a URL with one in its topic is rejected.
That includes a trailing #, such as mqtt://broker.example.com/home/#, which a URL reads as an empty fragment rather than part of the topic.
URL Formats¶
The MQTT service supports two URL schemes for connection security:
mqtt://: Standard unencrypted connection (port 1883 by default)
mqtt://[[USERNAME][:PASSWORD]@]HOST[:PORT]/TOPIC
mqtts://: TLS-encrypted connection (port 8883 by default)
mqtts://[[USERNAME][:PASSWORD]@]HOST[:PORT]/TOPIC
URL Fields¶
- Username - Auth username
Default: empty
URL part: User - Password - Auth password
Default: empty
URL part: Password - Host - MQTT broker hostname
Default:
localhost
URL part: Host - Port - MQTT broker port (default 8883 with TLS, 1883 without)
Default: empty
URL part: Port - Topic - Target topic name (Required)
URL part: Path
Query/Param Props¶
Props can be either supplied using the params argument or through the URL using
?key=value&key=value etc.
-
cleansession - Start with a clean session Default: ✔
yes -
clientid - MQTT client identifier (default: shoutrrr and 12 random characters, chosen per service) Default: empty
-
disabletls - Disable TLS encryption Default: ❌
no -
qos - Quality of Service level (0, 1, or 2) Default:
0Possible values:AtMostOnce,AtLeastOnce,ExactlyOnce -
retained - Retain message on broker Default: ❌
no -
skiptlsverify - Skip TLS certificate verification Default: ❌
noDeprecated:disabletlsverification
TLS Configuration Options
The following options control TLS behavior:
-
disabletls: When set toyes, forces an unencrypted connection even if themqtts://scheme is used. When set tono, connects with TLS even if themqtt://scheme is used. Either way, the key overrides the scheme, and a URL without a port uses 8883 with TLS and 1883 without it. -
skiptlsverify: When set toyes, skips TLS certificate verification while still using encryption. This is useful for self-signed certificates.
Shoutrrr logs a warning the first time it connects without TLS or without verifying the certificate.
In Go, Config.DisableTLS alone decides whether the connection uses TLS.
A Config built in code uses TLS unless DisableTLS is set, and connects to port 8883 when Port is 0.
GetURL writes the mqtts:// scheme for a config that uses TLS and mqtt:// for one that does not.
Security Warning: Silent TLS Downgrade
Setting disabletls=yes with mqtts:// will force an unencrypted connection despite the secure scheme. This is likely unexpected behavior and can cause silent downgrades where you believe traffic is encrypted but it is not.
Recommendation: If you intentionally want an unencrypted connection, use mqtt:// (non-TLS scheme) instead of combining mqtts:// with disabletls=yes.
Deprecated
The disabletlsverification parameter is deprecated in favor of skiptlsverify.
It still works and logs a warning the first time it is used, and skiptlsverify takes precedence when both are set. In Go, the Config.DisableTLSVerification field is likewise deprecated in favor of Config.SkipTLSVerify.
When to Use disabletls=yes
This option is intended for specific edge cases, such as:
- TLS-terminating proxy: When connecting through a proxy that handles TLS termination, where the connection from client-to-proxy uses TLS but proxy-to-broker is plain MQTT. For example, a reverse proxy like Traefik or nginx that terminates TLS and forwards to an internal MQTT broker.
- Testing environments: Local development where encryption is not required.
Connection Lifecycle¶
The service connects to the broker lazily and keeps the connection open between sends.
How It Works¶
Initialize()parses the URL and stores the configuration. It does not connect to the broker.- The first
Send()opens the connection, and later sends reuse it. - A connection that has not been used for 60 seconds is closed. The next
Send()opens a new one, so short-lived programs do not leave connections behind. Close()disconnects from the broker. A laterSend()reconnects.
A connection manager set with SetConnectionManager() belongs to the caller. The service never closes it for being idle, and Close() disconnects it.
Error Behavior¶
If opening the connection fails, the error is returned to the caller and the next Send() tries again, so temporary network issues or broker unavailability do not require a new call to Initialize(). If the connection ends, for example because it was disconnected, the next Send() opens a new one.
Message Size¶
A broker may announce the largest packet it accepts when the connection opens.
A send whose message would make a larger packet fails with ErrMessageTooLarge before anything is sent, since the broker would disconnect the client instead of delivering the message.
The packet also holds the topic and a few header bytes, so the largest message is a little smaller than the broker's limit.
A broker that announces no limit accepts packets up to about 256 MB, the largest MQTT can encode.
When a reconnection resumes the session, a message still waiting for its acknowledgement is resent unless it exceeds the new limit.
Shoutrrr drops such a message with a warning, and its send has already failed with the lost connection.
A connection manager set with SetConnectionManager() is not checked, since Shoutrrr does not see its broker's reply.
Client ID¶
The broker keeps one session per client ID. When a client connects with an ID that another connected client uses, the broker disconnects the other one.
Without a clientid, each service uses its own ID, such as shoutrrrK4QZ7M2XWB5D.
It is shoutrrr followed by 12 random letters and digits.
The service keeps it for every connection it opens, and Initialize() chooses a new one.
Set clientid in these cases:
- The broker accepts only known client IDs.
- A session should outlast its connection, with
cleansession=no.
Give each program that connects at the same time its own clientid.
The broker keeps a session after its connection ends as follows.
cleansession |
Shoutrrr closes the connection | The connection is lost |
|---|---|---|
yes (default) |
The broker discards the session at once, since no later connection resumes it | The broker keeps the session for 60 seconds. The automatic reconnect resumes it. A connection that a later send opens after an idle close starts a clean session, which discards it |
no |
The broker keeps the session for 60 seconds | The broker keeps the session for 60 seconds, so the reconnect resumes it |
Shoutrrr asks for the 60 seconds, and the broker may choose a different interval in its reply, including a shorter one.
Send Params¶
Params apply to a single send and do not change the service configuration. The qos and retained params affect every send that uses them. The connection params (clientid, cleansession, disabletls, and skiptlsverify) apply when that send opens the connection, and are ignored while an existing connection is reused.
// Publish this message as retained with QoS 1, without changing later sends
params := types.Params{
"qos": "1",
"retained": "yes",
}
service.Send(message, ¶ms)
Examples¶
Basic Notification
mqtt://broker.example.com/notifications
With Authentication
mqtt://USERNAME:[email protected]:1883/home/alerts
Secure Connection
mqtts://USERNAME:[email protected]:8883/home/alerts
With QoS and Retained Message
mqtt://broker.example.com/alerts?qos=1&retained=yes
Home Assistant
mqtt://homeassistant.local:1883/homeassistant/notification
Mosquitto broker with custom client ID
mqtt://mosquitto.example.com:1883/sensors/alerts?clientid=shoutrrr-alerts&qos=2
Self-signed Certificate
mqtts://broker.example.com:8883/secure/alerts?skiptlsverify=yes
Full Configuration
mqtts://USERNAME:[email protected]:8883/production/alerts?clientid=prod-shoutrrr&qos=1&retained=yes&cleansession=no