kmMqtt 0.0.1
MQTT Client Library
Loading...
Searching...
No Matches
kmMqtt::mqtt::MqttClient Class Reference

A high-level MQTT client interface for managing MQTT connections, publishing, subscribing, and handling events. More...

#include <MqttClient.h>

Public Member Functions

 MqttClient ()
 Constructs a default MqttClient instance.
 MqttClient (const IMqttEnvironment *const env, const MqttClientOptions &clientOptions)
 Constructs an MqttClient instance with a custom environment and options.
 ~MqttClient ()
 Destructor for the MqttClient class.
ReqResult connect (ConnectArgs &&args, ConnectAddress &&address) noexcept
 Connects to the MQTT broker using the provided connection arguments and address.
ReqResult publish (const char *topic, ByteBuffer &&payload, PublishOptions &&options) noexcept
 Publishes a message to a specified topic with the given payload and options.
ReqResult subscribe (const std::vector< Topic > &topics, SubscribeOptions &&options) noexcept
 Subscribe to the specified topics with the given subscribe options.
ReqResult unSubscribe (const std::vector< Topic > &topics, UnSubscribeOptions &&options) noexcept
 Unsubscribe from the specified topics with the given unsubscribe options.
ReqResult disconnect (DisconnectArgs &&args={}) noexcept
 Disconnects from the MQTT broker using the provided disconnect arguments.
ClientError shutdown () noexcept
 Shuts down the MQTT client, releasing all resources and stopping any ongoing operations.
ClientError tick () noexcept
 Ticks the MQTT client to process incoming and outgoing messages and events.
ErrorEvent & onErrorEvent () noexcept
 Accessor for the ErrorEvent.
ConnectEvent & onConnectEvent () noexcept
 Accessor for the ConnectEvent.
DisconnectEvent & onDisconnectEvent () noexcept
 Accessor for the DisconnectEvent.
ReconnectEvent & onReconnectEvent () noexcept
 Accessor for the ReconnectEvent.
PublishEvent & onPublishEvent () noexcept
 Accessor for the PublishEvent (receiving publish message from broker).
PublishCompletedEvent & onPublishCompletedEvent () noexcept
 Accessor for the PublishCompletedEvent.
SubscribeAckEvent & onSubscribeAckEvent () noexcept
 Accessor for the SubscribeAckEvent.
UnSubscribeAckEvent & onUnSubscribeAckEvent () noexcept
 Accessor for the UnSubscribeAckEvent.
ConnectionStatus getConnectionStatus () const noexcept
 Get the connection status of the MQTT client.
const MqttConnectionInfogetConnectionInfo () const noexcept
 Get the info of the current MQTT connection.
bool getIsTickAsync () const noexcept
 Check if the client is configured for asynchronous ticking.

Detailed Description

A high-level MQTT client interface for managing MQTT connections, publishing, subscribing, and handling events.

This class provides an abstraction for interacting with an MQTT broker. It supports connecting to the broker, publishing messages, subscribing to topics, and handling various MQTT events. The implementation details are encapsulated in the MqttClientImpl class.

Note
This class is non-copyable and non-movable.

Constructor & Destructor Documentation

◆ MqttClient() [1/2]

kmMqtt::mqtt::MqttClient::MqttClient ( )

Constructs a default MqttClient instance.

This constructor initializes the MQTT client with a default environment and options. It creates an instance of MqttClientImpl and manages its lifecycle.

◆ MqttClient() [2/2]

kmMqtt::mqtt::MqttClient::MqttClient ( const IMqttEnvironment *const env,
const MqttClientOptions & clientOptions )

Constructs an MqttClient instance with a custom environment and options.

This constructor allows the user to specify a custom environment and options for the MQTT client. If the client options specify asynchronous ticking, the client will start ticking asynchronously.

Parameters
envA pointer to the custom MQTT environment.
clientOptionsThe options for configuring the MQTT client.

◆ ~MqttClient()

kmMqtt::mqtt::MqttClient::~MqttClient ( )

Destructor for the MqttClient class.

Cleans up resources and ensures proper shutdown of the MQTT client. Safe to call without shutdown() first.

Member Function Documentation

◆ connect()

ReqResult kmMqtt::mqtt::MqttClient::connect ( ConnectArgs && args,
ConnectAddress && address )
noexcept

Connects to the MQTT broker using the provided connection arguments and address.

Parameters
argsThe connection arguments for establishing the MQTT connection.
addressThe address of the MQTT broker to connect to.
Returns
ReqResult indicating the result of the connection attempt.

◆ disconnect()

ReqResult kmMqtt::mqtt::MqttClient::disconnect ( DisconnectArgs && args = {})
noexcept

Disconnects from the MQTT broker using the provided disconnect arguments.

Parameters
argsThe disconnect arguments for terminating the MQTT connection.
Returns
ReqResult indicating the result of the disconnect attempt.

◆ getConnectionInfo()

const MqttConnectionInfo & kmMqtt::mqtt::MqttClient::getConnectionInfo ( ) const
noexcept

Get the info of the current MQTT connection.

Returns
MqttConnectionInfo containing details about the current connection.

◆ getConnectionStatus()

ConnectionStatus kmMqtt::mqtt::MqttClient::getConnectionStatus ( ) const
noexcept

Get the connection status of the MQTT client.

Returns
ConnectionStatus indicating the current connection status of the client.

◆ getIsTickAsync()

bool kmMqtt::mqtt::MqttClient::getIsTickAsync ( ) const
noexcept

Check if the client is configured for asynchronous ticking.

Returns
true if the client is set to tick asynchronously, false otherwise.

◆ onConnectEvent()

ConnectEvent & kmMqtt::mqtt::MqttClient::onConnectEvent ( )
noexcept

Accessor for the ConnectEvent.

Invoked when the client successfully connects to the MQTT broker. Invoked when the client fails to connect to the MQTT broker on initial connect call. Contains ConnectAck information if available.

Returns
Reference to the ConnectEvent instance.

◆ onDisconnectEvent()

DisconnectEvent & kmMqtt::mqtt::MqttClient::onDisconnectEvent ( )
noexcept

Accessor for the DisconnectEvent.

Invoked when the client disconnects from the MQTT broker. Invoked when the client fails to reconnect to the MQTT broker after an unexpected disconnection. Is not invoked as part of connect failure handling. Is not invoked as part of reconnection handling unless reconnection fails.

Returns
Reference to the DisconnectEvent instance.

◆ onErrorEvent()

ErrorEvent & kmMqtt::mqtt::MqttClient::onErrorEvent ( )
noexcept

Accessor for the ErrorEvent.

Invoked when an error occurs within the MQTT client.

Returns
Reference to the ErrorEvent instance.

◆ onPublishCompletedEvent()

PublishCompletedEvent & kmMqtt::mqtt::MqttClient::onPublishCompletedEvent ( )
noexcept

Accessor for the PublishCompletedEvent.

Invoked when a packet for publish acknowledged, release, received, or complete is received from the broker for a sent publish packet. Notifies of the general state of the publish process for its corresponding QOS level. Does not call back on QOS 0 publishes.

Returns
Reference to the PublishCompletedEvent instance.

◆ onPublishEvent()

PublishEvent & kmMqtt::mqtt::MqttClient::onPublishEvent ( )
noexcept

Accessor for the PublishEvent (receiving publish message from broker).

Invoked when a publish message is received for a subscribed topic.

Returns
Reference to the PublishEvent instance.

◆ onReconnectEvent()

ReconnectEvent & kmMqtt::mqtt::MqttClient::onReconnectEvent ( )
noexcept

Accessor for the ReconnectEvent.

Invoked when the client successfully reconnects to the MQTT broker after an unexpected disconnection. Invoked when reconnection starts after an unexpected disconnection.

Returns
Reference to the ReconnectEvent instance.

◆ onSubscribeAckEvent()

SubscribeAckEvent & kmMqtt::mqtt::MqttClient::onSubscribeAckEvent ( )
noexcept

Accessor for the SubscribeAckEvent.

Invoked when a subscribe acknowledgment is received from the broker for a sent subscribe packet.

Returns
Reference to the SubscribeAckEvent instance.

◆ onUnSubscribeAckEvent()

UnSubscribeAckEvent & kmMqtt::mqtt::MqttClient::onUnSubscribeAckEvent ( )
noexcept

Accessor for the UnSubscribeAckEvent.

Invoked when an unsubscribe acknowledgment is received from the broker for a sent unsubscribe packet.

Returns
Reference to the UnSubscribeAckEvent instance.

◆ publish()

ReqResult kmMqtt::mqtt::MqttClient::publish ( const char * topic,
ByteBuffer && payload,
PublishOptions && options )
noexcept

Publishes a message to a specified topic with the given payload and options.

Parameters
topicThe topic to which the message will be published.
payloadThe payload of the message to be published.
optionsThe options for publishing the message.
Returns
ReqResult indicating the result of the publish attempt.

◆ shutdown()

ClientError kmMqtt::mqtt::MqttClient::shutdown ( )
noexcept

Shuts down the MQTT client, releasing all resources and stopping any ongoing operations.

Use this for both in synchronous and asynchronous modes.

Returns
ClientError indicating the result of the shutdown operation.

◆ subscribe()

ReqResult kmMqtt::mqtt::MqttClient::subscribe ( const std::vector< Topic > & topics,
SubscribeOptions && options )
noexcept

Subscribe to the specified topics with the given subscribe options.

Parameters
topicsThe topics to which the client is subscribing.
optionsThe subscribe options.
Returns
ReqResult indicating the result of the subscribe attempt.

◆ tick()

ClientError kmMqtt::mqtt::MqttClient::tick ( )
noexcept

Ticks the MQTT client to process incoming and outgoing messages and events.

Use this method in synchronous mode to keep the client responsive. It's an error to call this method if the client is configured for asynchronous ticking.

Returns
ClientError indicating the result of the tick operation.

◆ unSubscribe()

ReqResult kmMqtt::mqtt::MqttClient::unSubscribe ( const std::vector< Topic > & topics,
UnSubscribeOptions && options )
noexcept

Unsubscribe from the specified topics with the given unsubscribe options.

Parameters
topicsThe topics from which the client is unsubscribing.
optionsThe unsubscribe options.
Returns
ReqResult indicating the result of the unsubscribe attempt.

The documentation for this class was generated from the following file: