An MQTT 3.1.1 / 5.0 client.
The process that creates a client owns it: received messages and connection losses arrive in its mailbox, and when it exits the client disconnects.
{:ok, client} = StemMqtt.Client.new(host: "localhost", client_id: "sensor-1")
{:ok, %{session_present: false}} = StemMqtt.Client.connect(client)
{:ok, _reason_code} = StemMqtt.Client.subscribe(client, "sensors/#", qos: 1)
:ok = StemMqtt.Client.publish(client, "sensors/t", "21.5", qos: 1)
receive do
{:stem_mqtt, id, {:message, %StemMqtt.Message{topic: topic, payload: payload}}}
when id == client.id ->
{topic, payload}
{:stem_mqtt, id, {:disconnected, reason}} when id == client.id ->
reason
endOptions
:host(required),:port(1883),:client_id("": the broker assigns one):version-:v5(default) or:v311:clean_start(true),:keep_alive_secs(30):username,:password(binary):will-%{topic: ..., payload: ..., qos: 0..2, retain: false}:connect_timeout_secs(10),:operation_timeout_secs(0: the default):auto_reconnect(false),:reconnect_backoff_secs,:reconnect_max_backoff_secs:tls-%{ca_cert_pem: ..., client_cert_pem: ..., client_key_pem: ..., insecure_skip_certificate_verification: false};%{}uses the system roots:max_packet_size(0: the default)
Unknown options raise ArgumentError.
Errors
Operations return {:error, {kind, message}} with kind one of
:malformed_packet, :protocol, :io, :connection_refused,
:not_connected, :packet_too_large, :already_connected, :timeout,
:session.
Summary
Functions
Why the last connection ended, or nil.
Creates a client owned by owner (default: the caller). Does not connect.
Options: :qos (0), :retain (false).
Options: :qos (0). Returns the broker's reason code (the granted QoS on success).
Types
@type t() :: %StemMqtt.Client{id: pos_integer(), ref: reference()}
id tags every message this client sends to its owner.
Functions
Why the last connection ended, or nil.
Creates a client owned by owner (default: the caller). Does not connect.
Options: :qos (0), :retain (false).
Options: :qos (0). Returns the broker's reason code (the granted QoS on success).