MqttierClient

Struct MqttierClient 

Source
pub struct MqttierClient {
    pub client_id: String,
    pub online_topic: String,
    /* private fields */
}
Expand description

MqttierClient provides an abstracted, Clone-able interface around rumqttc.

Usage contract:

  • Construct with MqttierClient::new(...).
  • Call run_loop().await once per client to start background tasks.
  • Use subscribe(...) to register a subscription and a channel sender for messages.
  • Use publish* helpers to publish data; they return a oneshot receiver that will be resolved when the publish completes (or timed out / error).

Fields§

§client_id: String§online_topic: String

Implementations§

Source§

impl MqttierClient

Source

pub fn new(mqttier_options: MqttierOptions) -> Result<Self, MqttierError>

Create a new MqttierClient.

§Arguments
  • hostname - The hostname of the MQTT broker
  • port - The port of the MQTT broker
  • client_id - Optional client ID. If None, a random UUID will be generated
Source

pub async fn subscribe( &self, topic: String, qos: u8, received_message_tx: Sender<ReceivedMessage>, ) -> Result<usize, MqttierError>

Subscribe to a topic.

§Arguments

Arguments:

  • topic: topic to subscribe to
  • qos: QoS level (0, 1, 2)
  • received_message_tx: mpsc Sender that will receive ReceivedMessages for this subscription

Returns: subscription id (usize) on success.

Source

pub async fn publish( &self, topic: String, payload: Vec<u8>, qos: QoS, retain: bool, publish_props: Option<PublishProperties>, ) -> Receiver<PublishResult>

Publish a raw payload to a topic.

§Arguments
  • topic - The topic to publish to

Arguments:

  • topic: topic to publish to
  • payload: raw payload bytes
  • qos: QoS (use rumqttc::v5::mqttbytes::QoS variants)
  • retain: whether to set the retain flag
  • publish_props: optional MQTT5 publish properties

Returns: a oneshot::Receiver<PublishResult> that will resolve when the publish completes or errors.

Source

pub async fn publish_string( &self, topic: String, payload: String, qos: u8, retain: bool, publish_props: Option<PublishProperties>, ) -> Receiver<PublishResult>

Publish a UTF-8 string payload to a topic.

§Arguments
  • topic - The topic to publish to
  • payload - The string payload to send
  • qos - The QoS level for the message (0, 1, or 2)
  • retain - Whether to retain the message
  • publish_props - Optional publish properties
§Returns

Returns a receiver that will be notified when the message is acknowledged by the broker

Source

pub async fn publish_structure<T: Serialize>( &self, topic: String, payload: T, ) -> Receiver<PublishResult>

Publish a serializable struct as JSON to a topic (QoS 2 by default in helper).

§Arguments
  • topic - The topic to publish to
  • payload - The serializable struct to send as payload
  • state_version - The version of the structure
§Returns

Returns a receiver that will be notified when the message is acknowledged by the broker

Source

pub async fn publish_request<T: Serialize>( &self, topic: String, payload: T, response_topic: String, correlation_id: Vec<u8>, ) -> Receiver<PublishResult>

Publish a request message to a topic with response topic and correlation id.

§Arguments
  • topic - The topic to publish to
  • payload - The serializable struct to send as payload
  • response_topic - The topic where responses should be sent
  • correlation_id - The correlation id for matching responses
§Returns

Returns a receiver that will be notified when the message is acknowledged by the broker

Source

pub async fn publish_response<T: Serialize>( &self, topic: String, payload: T, correlation_id: Vec<u8>, ) -> Receiver<PublishResult>

Publish a response message to a topic with correlation id.

§Arguments
  • topic - The topic to publish to
  • payload - The serializable struct to send as payload
  • correlation_id - The correlation id for matching requests
§Returns

Returns a receiver that will be notified when the message is acknowledged by the broker

Source

pub async fn publish_error_response( &self, topic: String, error_message: String, correlation_id: Vec<u8>, return_code: u32, ) -> Receiver<PublishResult>

Publish an error response message to a topic with correlation id.

This sends an empty JSON object {} as the payload, with user properties.

§Arguments
  • topic - The topic to publish to
  • error_message - The error message to send in the DebugMessage user property.
  • correlation_id - The correlation id for matching requests
  • return_code - The return code to send in the ReturnCode user property
§Returns

Returns a receiver that will be notified when the message is acknowledged by the broker.

Source

pub async fn publish_state<T: Serialize>( &self, topic: String, payload: T, state_version: u32, ) -> Receiver<PublishResult>

Publish a state message to a topic. Adds a user property PropertyVersion to the message properties.

§Arguments
  • topic - The topic to publish to
  • payload - The serializable struct to send as payload
§Returns

Returns a receiver that will be notified when the message is acknowledged by the broker

Source

pub async fn publish_status<T: Serialize>( &self, topic: String, payload: T, expiry_secs: u32, ) -> Receiver<PublishResult>

Source

pub async fn run_loop(&self) -> Result<(), MqttierError>

Start the run loop for handling MQTT connections and messages

Trait Implementations§

Source§

impl Clone for MqttierClient

Source§

fn clone(&self) -> MqttierClient

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

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

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

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

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
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

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