Closes the connection. By default sends a clean DISCONNECT; pass { graceful: false }
to drop the socket without one, which makes the broker publish the Last Will (used to
simulate an unexpected device disconnect).
Optionalopts: { graceful?: boolean }
graceful (default true): whether to send DISCONNECT first.
Resolves once the transport is closed.
Publishes a message to a topic.
For QoS 0 this resolves once the PUBLISH is written. For QoS 1 it resolves on the PUBACK, and for QoS 2 on the PUBCOMP, so the returned promise tracks broker confirmation.
The topic to publish to.
The message body (a string is UTF-8 encoded).
Optionalopts: { qos?: 0 | 1 | 2; retain?: boolean }
Optional QoS (default 0) and retain flag.
Resolves once the publish is confirmed at the requested QoS.
Publishes a value as JSON to a topic.
The value is JSON.stringify-ed and encoded as UTF-8, then published with publish.
The returned promise tracks broker confirmation at the requested QoS, exactly like
publish.
The topic to publish to.
The value to serialize and publish.
Optionalopts: { qos?: 0 | 1 | 2; retain?: boolean }
Optional QoS (default 0) and retain flag.
Resolves once the publish is confirmed at the requested QoS.
Subscribes to a topic filter.
Sends a SUBSCRIBE and returns a subscription whose async iterator yields matching messages. The SUBACK is awaited lazily; the first iteration (or MqttSubscription.unsubscribe) observes any broker rejection.
The topic filter (may contain + / # wildcards).
Optionalopts: { qos?: 0 | 1 | 2 }
Optional maximum QoS to request (default 0).
A subscription async iterable.
Subscribes to a topic filter and yields each message with its payload parsed from JSON.
A thin JSON layer over subscribe: each delivered message's payload is UTF-8 decoded
and JSON.parse-d. Iterate the result with for await. It is an AsyncDisposable, so
await using unsubscribes automatically.
The expected shape of each decoded value.
The topic filter (may contain + / # wildcards).
Optionalopts: { qos?: 0 | 1 | 2 }
Optional maximum QoS to request (default 0).
An async iterable of { topic, value }, also an AsyncDisposable.
Unsubscribes from a topic filter and ends its subscription.
The filter previously passed to subscribe.
Resolves once the UNSUBACK is received.
A live MQTT session over a single transport.
Obtain one from connect or connectWebSocket. A background pump reads packets and routes them, so publishing, subscribing, and the QoS acknowledgement handshakes can all be in flight at once. It is an
AsyncDisposable, soawait usingcloses it cleanly.Since
1.0.0