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().awaitonce 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: StringImplementations§
Source§impl MqttierClient
impl MqttierClient
Sourcepub fn new(mqttier_options: MqttierOptions) -> Result<Self, MqttierError>
pub fn new(mqttier_options: MqttierOptions) -> Result<Self, MqttierError>
Create a new MqttierClient.
§Arguments
hostname- The hostname of the MQTT brokerport- The port of the MQTT brokerclient_id- Optional client ID. If None, a random UUID will be generated
Sourcepub async fn subscribe(
&self,
topic: String,
qos: u8,
received_message_tx: Sender<ReceivedMessage>,
) -> Result<usize, MqttierError>
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 toqos: QoS level (0, 1, 2)received_message_tx: mpsc Sender that will receiveReceivedMessages for this subscription
Returns: subscription id (usize) on success.
Sourcepub async fn publish(
&self,
topic: String,
payload: Vec<u8>,
qos: QoS,
retain: bool,
publish_props: Option<PublishProperties>,
) -> Receiver<PublishResult>
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 topayload: raw payload bytesqos: QoS (userumqttc::v5::mqttbytes::QoSvariants)retain: whether to set the retain flagpublish_props: optional MQTT5 publish properties
Returns: a oneshot::Receiver<PublishResult> that will resolve when the publish
completes or errors.
Sourcepub async fn publish_string(
&self,
topic: String,
payload: String,
qos: u8,
retain: bool,
publish_props: Option<PublishProperties>,
) -> Receiver<PublishResult>
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 topayload- The string payload to sendqos- The QoS level for the message (0, 1, or 2)retain- Whether to retain the messagepublish_props- Optional publish properties
§Returns
Returns a receiver that will be notified when the message is acknowledged by the broker
Sourcepub async fn publish_structure<T: Serialize>(
&self,
topic: String,
payload: T,
) -> Receiver<PublishResult>
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 topayload- The serializable struct to send as payloadstate_version- The version of the structure
§Returns
Returns a receiver that will be notified when the message is acknowledged by the broker
Sourcepub async fn publish_request<T: Serialize>(
&self,
topic: String,
payload: T,
response_topic: String,
correlation_id: Vec<u8>,
) -> Receiver<PublishResult>
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 topayload- The serializable struct to send as payloadresponse_topic- The topic where responses should be sentcorrelation_id- The correlation id for matching responses
§Returns
Returns a receiver that will be notified when the message is acknowledged by the broker
Sourcepub async fn publish_response<T: Serialize>(
&self,
topic: String,
payload: T,
correlation_id: Vec<u8>,
) -> Receiver<PublishResult>
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 topayload- The serializable struct to send as payloadcorrelation_id- The correlation id for matching requests
§Returns
Returns a receiver that will be notified when the message is acknowledged by the broker
Sourcepub async fn publish_error_response(
&self,
topic: String,
error_message: String,
correlation_id: Vec<u8>,
return_code: u32,
) -> Receiver<PublishResult>
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 toerror_message- The error message to send in theDebugMessageuser property.correlation_id- The correlation id for matching requestsreturn_code- The return code to send in theReturnCodeuser property
§Returns
Returns a receiver that will be notified when the message is acknowledged by the broker.
Sourcepub async fn publish_state<T: Serialize>(
&self,
topic: String,
payload: T,
state_version: u32,
) -> Receiver<PublishResult>
pub async fn publish_state<T: Serialize>( &self, topic: String, payload: T, state_version: u32, ) -> Receiver<PublishResult>
pub async fn publish_status<T: Serialize>( &self, topic: String, payload: T, expiry_secs: u32, ) -> Receiver<PublishResult>
Sourcepub async fn run_loop(&self) -> Result<(), MqttierError>
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
impl Clone for MqttierClient
Source§fn clone(&self) -> MqttierClient
fn clone(&self) -> MqttierClient
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more