class

MQTT::Client

Inherits Reference / Object

Connects with MQTT 5.0 and falls back to 3.1.1 when the broker will not take it, exposing what the two protocols have in common.

A broker that rejects the version closes the connection, so negotiation needs a fresh transport for the retry. That is why this takes a factory rather than a transport:

client = MQTT::Client.new { MQTT::Transport::TCP.new("test.mosquitto.org") }
client.connect
client.version # => MQTT::Version::V5

Version specific features stay on MQTT::V5::Client, reachable through #v5 once you know what you are talking to.

Constructors

new(timeout : Time::Span | Nil = MQTT::ClientBase::DEFAULT_TIMEOUT, max_packet_size : UInt32 = MQTT::DEFAULT_MAX_PACKET_SIZE, reconnect : MQTT::Reconnect = MQTT::Reconnect.new, &factory : -> Transport)
Source

Instance methods

closed?
Source
connect(username : String | Nil = nil, password : String | Nil = nil, keep_alive : Int32 = 60, client_id : String = MQTT.generate_client_id, clean_start : Bool = true, will_flag : Bool = false, will_qos : Int32 | QoS = 0, will_retain : Bool = false, will_topic : String | Nil = nil, will_payload : String | Bytes | Nil = nil, timeout : Time::Span | Nil = @timeout, keep_alive_active : Bool = true) : Version

Tries 5.0, then 3.1.1. Once a version is settled the underlying client keeps the factory, so reconnections use it directly and never re-probe

Source
disconnect(send_msg = true) : Nil
Source
last_ping_response
Source
negotiated

The negotiated client. Raises until #connect has succeeded

Source
ping(timeout : Time::Span | Nil = @timeout) : Nil
Source
publish(topic : String, payload = "", retain : Bool = false, qos : QoS = QoS::FireAndForget, timeout : Time::Span | Nil = @timeout)
Source
session_present?

Whether the broker resumed an existing session

Source
subscribe(*topics, qos : QoS = QoS::FireAndForget, timeout : Time::Span | Nil = @timeout, &callback : String, Bytes, Bool -> Nil)
Source
subscriptions
Source
terminated?
Source
unsubscribe(*topics, timeout : Time::Span | Nil = @timeout)
Source
v3

The 3.1.1 client, when that is what was negotiated

Source
v5

The 5.0 client, when 5.0 is what was negotiated

Source
version

The version actually negotiated, nil until #connect has run

Source
wait_close
Source

Nested types