connect

suspend fun connect(endpoint: MqttEndpoint)

Connect to the MQTT broker at the specified endpoint.

Performs the CONNECT/CONNACK handshake. On success, starts forwarding incoming messages to messages and connection state changes to connectionState.

If the broker responds with a server redirect (reason code USE_ANOTHER_SERVER or SERVER_MOVED with a Server Reference property), the client automatically follows the redirect up to MAX_REDIRECTS times (§4.13).

Failures are classified by which side of the handshake produced them, so a caller can tell an unusable configuration from a bad network and pick a retry policy accordingly.

Parameters

endpoint

Broker endpoint (TCP or WebSocket).

Throws

if the broker answered with a CONNACK carrying an error reason code — including an unsupported protocol version that could not be renegotiated. Retrying the same configuration will fail the same way.

if the transport failed before any CONNACK arrived (DNS, TCP, TLS, a socket closed mid-handshake, or a CONNACK timeout). The broker never expressed an opinion, so a retry may succeed.

if the broker answered but violated the spec.

SwallowedException is suppressed deliberately: the internal exception is not chained as the cause, because toConnectException carries over its reason code, message, server reference and its own cause, and the internal type is not part of the public API.