Skip to content

Security configuration#

MQTTBroker accepts the same security object pattern as other FastStream brokers where supported.

TLS#

Use the URL scheme to select the transport. mqtt:// uses plain TCP and defaults to port 1883; mqtts:// enables TLS and defaults to port 8883.

For custom TLS settings, pass an SSL context with BaseSecurity.

Username and password#

Provide percent-encoded credentials in the URL or use SASLPlaintext from faststream.security.

1
2
3
4
5
6
from faststream.mqtt import MQTTBroker
from faststream.security import SASLPlaintext

security = SASLPlaintext(username="device", password="secret")
broker = MQTTBroker("mqtts://mqtt.example.com", security=security)
broker_from_url = MQTTBroker("mqtts://device:secret@mqtt.example.com")

Unsupported security subclasses raise NotImplementedError at broker construction time.

Note

MQTT connection URLs support only mqtt:// and mqtts:// TCP/TLS endpoints. Paths, query parameters, and fragments are rejected.