class MQTT::Client

Overview

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.

Defined in:

mqtt/client.cr

Constructors

Instance Method Summary

Constructor Detail

def self.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) #

[View source]

Instance Method Detail

def closed? : Bool #

[View source]
def 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


[View source]
def disconnect(send_msg = true) : Nil #

[View source]
def last_ping_response : Time | Nil #

[View source]
def negotiated : V3::Client | V5::Client #

The negotiated client. Raises until #connect has succeeded


[View source]
def ping(timeout : Time::Span | Nil = @timeout) : Nil #

[View source]
def publish(topic : String, payload = "", retain : Bool = false, qos : QoS = QoS::FireAndForget, timeout : Time::Span | Nil = @timeout) #

[View source]
def session_present? : Bool #

Whether the broker resumed an existing session


[View source]
def subscribe(*topics, qos : QoS = QoS::FireAndForget, timeout : Time::Span | Nil = @timeout, &callback : String, Bytes, Bool -> Nil) #

[View source]
def subscriptions : Hash(String, QoS) #

[View source]
def terminated? : Bool #

[View source]
def unsubscribe(*topics, timeout : Time::Span | Nil = @timeout) #

[View source]
def v3 : V3::Client | Nil #

The 3.1.1 client, when that is what was negotiated


[View source]
def v5 : V5::Client | Nil #

The 5.0 client, when 5.0 is what was negotiated


[View source]
def version : Version | Nil #

The version actually negotiated, nil until #connect has run


[View source]
def wait_close : Nil #

[View source]