Skip to main content

MqttTransport

Struct MqttTransport 

Source
pub struct MqttTransport { /* private fields */ }
Expand description

An MQTT client that implements the core [Transport] trait.

A transport is created disconnected; connect opens the link and spawns the background task that runs the MQTT event loop for the life of the connection. Inbound messages are queued and read with recv.

Implementations§

Source§

impl MqttTransport

Source

pub fn new(config: MqttConfig) -> Self

Creates a transport from the given configuration without connecting.

§Arguments
  • config - the broker connection settings.
§Returns

A disconnected transport ready for connect.

Source

pub fn is_connected(&self) -> bool

Reports whether the transport currently holds an active connection.

§Returns

true once connect has succeeded and before disconnect is called.

Source

pub async fn recv(&mut self) -> Result<Option<Message>>

Awaits the next message from any subscribed topic.

§Returns

Some(message) for the next queued message, or None once the event loop has stopped and no further messages will arrive.

§Errors

Returns [Error::Closed] if the transport is not connected.

Source

pub async fn disconnect(&mut self) -> Result<()>

Closes the connection and stops the background event loop.

Calling this on a transport that is not connected is a no-op.

§Returns

Ok(()) once the disconnect request has been issued and the event loop task has been stopped.

§Errors

This call is best-effort and does not surface broker errors raised while tearing down, so it currently always returns Ok(()).

Trait Implementations§

Source§

impl Transport for MqttTransport

Source§

async fn connect(&mut self) -> Result<()>

Establishes the connection to the broker, peer, or bus. Read more
Source§

async fn send(&mut self, topic: &str, payload: &[u8]) -> Result<()>

Publishes a payload to a topic. Read more
Source§

async fn subscribe(&mut self, topic: &str) -> Result<()>

Subscribes to a topic so that matching payloads are routed to this transport. 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
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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.