pub struct Client { /* private fields */ }
Expand description
MQTT Client with a synchronous (blocking) API. This is simply a convenience wrapper around the asynchronous API, providing blocking calls with timeouts.
Implementations
sourceimpl Client
impl Client
sourcepub fn new<T>(opts: T) -> Result<Client> where
T: Into<CreateOptions>,
pub fn new<T>(opts: T) -> Result<Client> where
T: Into<CreateOptions>,
Creates a new MQTT client which can connect to an MQTT broker.
sourcepub fn set_timeout(&mut self, timeout: Duration)
pub fn set_timeout(&mut self, timeout: Duration)
Sets the default timeout used for synchronous operations.
Arguments
timeout
The timeout to use for synchronous calls, like
connect(), disconnect(), publish(), etc.
sourcepub fn connect<T>(&self, opt_opts: T) -> Result<ServerResponse> where
T: Into<Option<ConnectOptions>>,
pub fn connect<T>(&self, opt_opts: T) -> Result<ServerResponse> where
T: Into<Option<ConnectOptions>>,
Connects to an MQTT broker using the specified connect options.
sourcepub fn disconnect<T>(&self, opt_opts: T) -> Result<()> where
T: Into<Option<DisconnectOptions>>,
pub fn disconnect<T>(&self, opt_opts: T) -> Result<()> where
T: Into<Option<DisconnectOptions>>,
Disconnects from the MQTT broker.
Arguments
opt_opts
Optional disconnect options. Specifying None
will use
default of immediate (zero timeout) disconnect.
sourcepub fn disconnect_after(&self, timeout: Duration) -> Result<()>
pub fn disconnect_after(&self, timeout: Duration) -> Result<()>
Disconnect from the MQTT broker with a timeout. This will delay the disconnect for up to the specified timeout to allow in-flight messages to complete. This is the same as calling disconnect with options specifying a timeout.
Arguments
timeout
The amount of time to wait for the disconnect. This has
a resolution in milliseconds.
sourcepub fn reconnect(&self) -> Result<ServerResponse>
pub fn reconnect(&self) -> Result<ServerResponse>
Attempts to reconnect to the broker. This can only be called after a connection was initially made or attempted. It will retry with the same connect options.
sourcepub fn is_connected(&self) -> bool
pub fn is_connected(&self) -> bool
Determines if this client is currently connected to an MQTT broker.
sourcepub fn subscribe(&self, topic: &str, qos: i32) -> Result<ServerResponse>
pub fn subscribe(&self, topic: &str, qos: i32) -> Result<ServerResponse>
Subscribes to a single topic.
Arguments
topic
The topic name
qos
The quality of service requested for messages
sourcepub fn subscribe_with_options<S, T, P>(
&self,
topic: S,
qos: i32,
opts: T,
props: P
) -> Result<ServerResponse> where
S: Into<String>,
T: Into<SubscribeOptions>,
P: Into<Option<Properties>>,
pub fn subscribe_with_options<S, T, P>(
&self,
topic: S,
qos: i32,
opts: T,
props: P
) -> Result<ServerResponse> where
S: Into<String>,
T: Into<SubscribeOptions>,
P: Into<Option<Properties>>,
Subscribes to a single topic with v5 options
Arguments
topic
The topic name
qos
The quality of service requested for messages
opts
Options for the subscription
props
MQTT v5 properties
sourcepub fn subscribe_many<T>(
&self,
topics: &[T],
qos: &[i32]
) -> Result<ServerResponse> where
T: AsRef<str>,
pub fn subscribe_many<T>(
&self,
topics: &[T],
qos: &[i32]
) -> Result<ServerResponse> where
T: AsRef<str>,
Subscribes to multiple topics simultaneously.
Arguments
topic
The topic name
qos
The quality of service requested for messages
sourcepub fn subscribe_many_with_options<T, P>(
&self,
topics: &[T],
qos: &[i32],
opts: &[SubscribeOptions],
props: P
) -> Result<ServerResponse> where
T: AsRef<str>,
P: Into<Option<Properties>>,
pub fn subscribe_many_with_options<T, P>(
&self,
topics: &[T],
qos: &[i32],
opts: &[SubscribeOptions],
props: P
) -> Result<ServerResponse> where
T: AsRef<str>,
P: Into<Option<Properties>>,
Subscribes to multiple topics simultaneously with options.
Arguments
topics
The collection of topic names
qos
The quality of service requested for messages
opts
Subscribe options (one per topic)
props
MQTT v5 properties
sourcepub fn unsubscribe(&self, topic: &str) -> Result<()>
pub fn unsubscribe(&self, topic: &str) -> Result<()>
Unsubscribes from a single topic.
Arguments
topic
The topic to unsubscribe. It must match a topic from a
previous subscribe.
sourcepub fn unsubscribe_with_options<S>(
&self,
topic: S,
props: Properties
) -> Result<()> where
S: Into<String>,
pub fn unsubscribe_with_options<S>(
&self,
topic: S,
props: Properties
) -> Result<()> where
S: Into<String>,
Unsubscribes from a single topic.
Arguments
topic
The topic to unsubscribe. It must match a topic from a
previous subscribe.
props
MQTT v5 properties for the unsubscribe.
sourcepub fn unsubscribe_many<T>(&self, topics: &[T]) -> Result<()> where
T: AsRef<str>,
pub fn unsubscribe_many<T>(&self, topics: &[T]) -> Result<()> where
T: AsRef<str>,
Unsubscribes from multiple topics simultaneously.
Arguments
topic
The topics to unsubscribe. Each must match a topic from a
previous subscribe.
sourcepub fn unsubscribe_many_with_options<T>(
&self,
topics: &[T],
props: Properties
) -> Result<()> where
T: AsRef<str>,
pub fn unsubscribe_many_with_options<T>(
&self,
topics: &[T],
props: Properties
) -> Result<()> where
T: AsRef<str>,
Unsubscribes from multiple topics simultaneously.
Arguments
topic
The topics to unsubscribe. Each must match a topic from a
previous subscribe.
props
MQTT v5 properties for the unsubscribe.
sourcepub fn start_consuming(&self) -> Receiver<Option<Message>>
pub fn start_consuming(&self) -> Receiver<Option<Message>>
Starts the client consuming messages.
This starts the client receiving messages and placing them into an mpsc queue. It returns the receiving-end of the queue for the application to get the messages. This can be called at any time after the client is created, but it should be called before subscribing to any topics, otherwise messages can be lost.
sourcepub fn stop_consuming(&self)
pub fn stop_consuming(&self)
Stops the client consumer.
Trait Implementations
Auto Trait Implementations
impl !RefUnwindSafe for Client
impl Send for Client
impl Sync for Client
impl Unpin for Client
impl !UnwindSafe for Client
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more