Skip to main content

ClientOptions

Struct ClientOptions 

#[non_exhaustive]
pub struct ClientOptions {
Show 13 fields pub client_id: MqttString, pub keep_alive: u16, pub clean_start: CleanStart, pub username: Option<MqttString>, pub password: Option<MqttBytesBuf>, pub session_expiry: Option<u32>, pub receive_maximum: Option<NonZero<u16>>, pub topic_alias_maximum: Option<u16>, pub maximum_packet_size: Option<NonZero<u32>>, pub connect_timeout: Option<Duration>, pub ack_timeout: Option<Duration>, pub will: Option<WillOptions>, pub reconnect: Option<ReconnectOptions>,
}
Expand description

Options for the MQTT v5 client.

Controls the CONNECT packet fields, v5 session properties, and automatic reconnection behaviour.

Construct it with ClientOptions::new and the builder methods.

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§client_id: MqttString

Client identifier. An empty string lets the broker assign one.

§keep_alive: u16

Keep-alive interval in seconds. 0 disables keep-alive.

§clean_start: CleanStart

Whether to request a clean start on the initial connect.

§username: Option<MqttString>

Optional username for authentication.

§password: Option<MqttBytesBuf>

Optional password for authentication.

§session_expiry: Option<u32>

Session expiry interval in seconds (property 0x11). None = not sent. Some(0) = session ends on disconnect.

§receive_maximum: Option<NonZero<u16>>

Maximum number of in-flight QoS 1/2 messages this client can handle (property 0x21).

§topic_alias_maximum: Option<u16>

Maximum topic alias value this client supports (property 0x22). None or Some(0) = topic aliases disabled.

§maximum_packet_size: Option<NonZero<u32>>

Maximum packet size this client is willing to accept (property 0x27).

The client advertises this value to the broker and enforces it on receive. It rejects an inbound packet whose declared size exceeds this with DISCONNECT (PacketTooLarge) before it buffers the body. A peer cannot force an unbounded allocation. Defaults to DEFAULT_MAX_PACKET_SIZE. Raise it for large-payload workloads.

§connect_timeout: Option<Duration>

Deadline for one connection attempt: TCP connect plus the CONNECT / CONNACK handshake. If it elapses the attempt is abandoned (and retried when reconnection is enabled), so a broker that accepts the socket but never answers cannot hang the client. None disables it.

§ack_timeout: Option<Duration>

Deadline for an in-flight QoS >= 1 operation to be acknowledged. If a PUBACK/PUBCOMP (publish) or SUBACK/UNSUBACK (subscribe/unsubscribe) does not arrive within this duration of the packet being written, the connection is treated as dead and torn down; reconnection then resumes the session and retransmits the unacknowledged operations.

This is a liveness signal tied to real traffic, independent of keep_alive: it detects a broker that keeps the socket alive (and may even send unrelated inbound traffic) but never acknowledges your operations, which the keep-alive probe cannot.

It only provides coverage while operations are in flight. With no outstanding QoS >= 1 operation there is nothing to time out, so an idle gap is not covered. Setting keep_alive to 0 and relying solely on this is safe only under a workload with continuous QoS >= 1 traffic. Defaults to DEFAULT_ACK_TIMEOUT; None disables it.

§will: Option<WillOptions>

Will message configuration. None = no will message.

§reconnect: Option<ReconnectOptions>

Reconnect strategy. None disables automatic reconnection.

Implementations§

Source§

impl ClientOptions

Source

pub const fn new(client_id: MqttString) -> ClientOptions

Creates minimal options with a client ID.

Defaults: keep-alive 60 s and CleanStart::Auto. Authentication and reconnection are off. The size limit for inbound packets is DEFAULT_MAX_PACKET_SIZE, the handshake deadline is DEFAULT_CONNECT_TIMEOUT and the ack deadline is DEFAULT_ACK_TIMEOUT.

Source

pub const fn connect_timeout(self, timeout: Duration) -> ClientOptions

Sets the connection-attempt deadline (TCP connect + CONNACK handshake).

Source

pub const fn keep_alive(self, secs: u16) -> ClientOptions

Sets the keep-alive interval in seconds.

Source

pub const fn ack_timeout(self, timeout: Duration) -> ClientOptions

Sets the ack_timeout for in-flight QoS >= 1 operations.

Source

pub const fn clean_start(self, clean_start: CleanStart) -> ClientOptions

Sets whether to request a clean start on the initial connect.

Source

pub fn username(self, user: MqttString) -> ClientOptions

Sets the username for authentication.

Source

pub fn password(self, pass: MqttBytesBuf) -> ClientOptions

Sets the password for authentication.

Source

pub const fn session_expiry(self, seconds: u32) -> ClientOptions

Sets the session expiry interval in seconds (v5 property 0x11).

Source

pub const fn receive_maximum(self, max: NonZero<u16>) -> ClientOptions

Sets the receive maximum (v5 property 0x21).

Source

pub const fn topic_alias_maximum(self, max: u16) -> ClientOptions

Sets the topic alias maximum this client supports (v5 property 0x22).

Source

pub const fn maximum_packet_size(self, max: NonZero<u32>) -> ClientOptions

Sets the maximum packet size this client will accept (v5 property 0x27).

Source

pub fn will(self, topic: TopicBuf, payload: MqttBytesBuf) -> ClientOptions

Sets a will message (Last Will and Testament).

The broker publishes this message if the client disconnects ungracefully.

Source

pub fn will_with(self, will: WillOptions) -> ClientOptions

Sets a will message with full configuration.

Source

pub fn reconnect(self) -> ClientOptions

Enables automatic reconnection with default settings.

Source

pub const fn reconnect_with(self, opts: ReconnectOptions) -> ClientOptions

Enables automatic reconnection with custom settings.

Trait Implementations§

Source§

impl Debug for ClientOptions

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more