Paho C++  1.0
The Paho MQTT C++ Client Library
 All Classes Files Functions Variables Typedefs Friends
mqtt::client Class Reference

Lightweight client for talking to an MQTT server using methods that block until an operation completes. More...

#include <client.h>

Inheritance diagram for mqtt::client:
mqtt::callback

Public Types

using ptr_t = std::shared_ptr< client >
 Smart pointer type for this object.
 
using qos_collection = async_client::qos_collection
 Type for a collection of QOS values.
 

Public Member Functions

 client (const string &serverURI, const string &clientId, iclient_persistence *persistence=nullptr)
 Create a client that can be used to communicate with an MQTT server. More...
 
 client (const string &serverURI, const string &clientId, const string &persistDir)
 Create an async_client that can be used to communicate with an MQTT server. More...
 
 client (const string &serverURI, const string &clientId, int maxBufferedMessages, iclient_persistence *persistence=nullptr)
 Create a client that can be used to communicate with an MQTT server, which allows for off-line message buffering. More...
 
 client (const string &serverURI, const string &clientId, int maxBufferedMessages, const string &persistDir)
 Create a client that can be used to communicate with an MQTT server, which allows for off-line message buffering. More...
 
virtual ~client ()
 Virtual destructor.
 
virtual void connect ()
 Connects to an MQTT server using the default options.
 
virtual void connect (connect_options opts)
 Connects to an MQTT server using the specified options. More...
 
virtual void reconnect ()
 Reconnects the client using options from the previous connect. More...
 
virtual void disconnect ()
 Disconnects from the server.
 
virtual void disconnect (int timeoutMS)
 Disconnects from the server. More...
 
template<class Rep , class Period >
void disconnect (const std::chrono::duration< Rep, Period > &to)
 Disconnects from the server. More...
 
virtual string get_client_id () const
 Gets the client ID used by this client. More...
 
virtual string get_server_uri () const
 Gets the address of the server used by this client. More...
 
virtual std::chrono::milliseconds get_timeout () const
 Return the maximum time to wait for an action to complete. More...
 
virtual topic get_topic (const string &top)
 Get a topic object which can be used to publish messages on this client. More...
 
virtual bool is_connected () const
 Determines if this client is currently connected to the server. More...
 
virtual void publish (string_ref top, const void *payload, size_t n, int qos, bool retained)
 Publishes a message to a topic on the server and return once it is delivered. More...
 
virtual void publish (string_ref top, const void *payload, size_t n)
 Publishes a message to a topic on the server and return once it is delivered. More...
 
virtual void publish (const_message_ptr msg)
 Publishes a message to a topic on the server. More...
 
virtual void publish (const message &msg)
 Publishes a message to a topic on the server. More...
 
virtual void set_callback (callback &cb)
 Sets the callback listener to use for events that happen asynchronously. More...
 
virtual void set_timeout (int timeoutMS)
 Set the maximum time to wait for an action to complete. More...
 
template<class Rep , class Period >
void set_timeout (const std::chrono::duration< Rep, Period > &to)
 Set the maximum time to wait for an action to complete. More...
 
virtual void subscribe (const string &topicFilter)
 Subscribe to a topic, which may include wildcards using a QoS of 1. More...
 
virtual void subscribe (const string &topicFilter, int qos)
 Subscribe to a topic, which may include wildcards. More...
 
virtual void subscribe (const string_collection &topicFilters)
 Subscribes to a one or more topics, which may include wildcards using a QoS of 1. More...
 
virtual void subscribe (const string_collection &topicFilters, const qos_collection &qos)
 Subscribes to multiple topics, each of which may include wildcards. More...
 
virtual void unsubscribe (const string &topicFilter)
 Requests the server unsubscribe the client from a topic. More...
 
virtual void unsubscribe (const string_collection &topicFilters)
 Requests the server unsubscribe the client from one or more topics. More...
 
void start_consuming ()
 Start consuming messages. More...
 
void stop_consuming ()
 Stop consuming messages. More...
 
const_message_ptr consume_message ()
 Read the next message from the queue. More...
 
bool try_consume_message (const_message_ptr *msg)
 Try to read the next message from the queue without blocking. More...
 
template<typename Rep , class Period >
bool try_consume_message_for (const_message_ptr *msg, const std::chrono::duration< Rep, Period > &relTime)
 Waits a limited time for a message to arrive. More...
 
template<class Clock , class Duration >
bool try_consume_message_until (const_message_ptr *msg, const std::chrono::time_point< Clock, Duration > &absTime)
 Waits until a specific time for a message to occur. More...
 

Detailed Description

Lightweight client for talking to an MQTT server using methods that block until an operation completes.

Constructor & Destructor Documentation

mqtt::client::client ( const string &  serverURI,
const string &  clientId,
iclient_persistence persistence = nullptr 
)

Create a client that can be used to communicate with an MQTT server.

This allows the caller to specify a user-defined persistence object, or use no persistence.

Parameters
serverURIthe address of the server to connect to, specified as a URI.
clientIda client identifier that is unique on the server being connected to
persistenceThe user persistence structure. If this is null, then no persistence is used.
mqtt::client::client ( const string &  serverURI,
const string &  clientId,
const string &  persistDir 
)

Create an async_client that can be used to communicate with an MQTT server.

This uses file-based persistence in the specified directory.

Parameters
serverURIthe address of the server to connect to, specified as a URI.
clientIda client identifier that is unique on the server being connected to
persistDirThe directory to use for persistence data
mqtt::client::client ( const string &  serverURI,
const string &  clientId,
int  maxBufferedMessages,
iclient_persistence persistence = nullptr 
)

Create a client that can be used to communicate with an MQTT server, which allows for off-line message buffering.

This allows the caller to specify a user-defined persistence object, or use no persistence.

Parameters
serverURIthe address of the server to connect to, specified as a URI.
clientIda client identifier that is unique on the server being connected to
maxBufferedMessagesthe maximum number of messages allowed to be buffered while not connected
persistenceThe user persistence structure. If this is null, then no persistence is used.
mqtt::client::client ( const string &  serverURI,
const string &  clientId,
int  maxBufferedMessages,
const string &  persistDir 
)

Create a client that can be used to communicate with an MQTT server, which allows for off-line message buffering.

This uses file-based persistence in the specified directory.

Parameters
serverURIthe address of the server to connect to, specified as a URI.
clientIda client identifier that is unique on the server being connected to
maxBufferedMessagesthe maximum number of messages allowed to be buffered while not connected
persistDirThe directory to use for persistence data

Member Function Documentation

virtual void mqtt::client::connect ( connect_options  opts)
inlinevirtual

Connects to an MQTT server using the specified options.

Parameters
opts
const_message_ptr mqtt::client::consume_message ( )
inline

Read the next message from the queue.

This blocks until a new message arrives.

Returns
The message and topic.
virtual void mqtt::client::disconnect ( int  timeoutMS)
inlinevirtual

Disconnects from the server.

Parameters
timeoutMSthe amount of time in milliseconds to allow for existing work to finish before disconnecting. A value of zero or less means the client will not quiesce.
template<class Rep , class Period >
void mqtt::client::disconnect ( const std::chrono::duration< Rep, Period > &  to)
inline

Disconnects from the server.

Parameters
tothe amount of time in milliseconds to allow for existing work to finish before disconnecting. A value of zero or less means the client will not quiesce.
virtual string mqtt::client::get_client_id ( ) const
inlinevirtual

Gets the client ID used by this client.

Returns
The client ID used by this client.
virtual string mqtt::client::get_server_uri ( ) const
inlinevirtual

Gets the address of the server used by this client.

Returns
The address of the server used by this client, as a URI.
virtual std::chrono::milliseconds mqtt::client::get_timeout ( ) const
inlinevirtual

Return the maximum time to wait for an action to complete.

Returns
int
virtual topic mqtt::client::get_topic ( const string &  top)
inlinevirtual

Get a topic object which can be used to publish messages on this client.

Parameters
topThe topic name
Returns
A topic attached to this client.
virtual bool mqtt::client::is_connected ( ) const
inlinevirtual

Determines if this client is currently connected to the server.

Returns
true if the client is currently connected, false if not.
virtual void mqtt::client::publish ( string_ref  top,
const void *  payload,
size_t  n,
int  qos,
bool  retained 
)
inlinevirtual

Publishes a message to a topic on the server and return once it is delivered.

Parameters
topThe topic to publish
payloadThe data to publish
nThe size in bytes of the data
qosThe QoS for message delivery
retainedWhether the broker should retain the message
virtual void mqtt::client::publish ( string_ref  top,
const void *  payload,
size_t  n 
)
inlinevirtual

Publishes a message to a topic on the server and return once it is delivered.

Parameters
topThe topic to publish
payloadThe data to publish
nThe size in bytes of the data
virtual void mqtt::client::publish ( const_message_ptr  msg)
inlinevirtual

Publishes a message to a topic on the server.

Parameters
msgThe message
virtual void mqtt::client::publish ( const message msg)
inlinevirtual

Publishes a message to a topic on the server.

This version will not timeout since that could leave the library with a reference to memory that could disappear while the library is still using it.

Parameters
msgThe message
virtual void mqtt::client::reconnect ( )
inlinevirtual

Reconnects the client using options from the previous connect.

The client must have previously called connect() for this to work.

virtual void mqtt::client::set_callback ( callback cb)
virtual

Sets the callback listener to use for events that happen asynchronously.

Parameters
cbThe callback functions
virtual void mqtt::client::set_timeout ( int  timeoutMS)
inlinevirtual

Set the maximum time to wait for an action to complete.

Parameters
timeoutMSThe timeout in milliseconds
template<class Rep , class Period >
void mqtt::client::set_timeout ( const std::chrono::duration< Rep, Period > &  to)
inline

Set the maximum time to wait for an action to complete.

Parameters
toThe timeout as a std::chrono duration.
void mqtt::client::start_consuming ( )
inline

Start consuming messages.

This initializes the client to receive messages through a queue that can be read synchronously.

void mqtt::client::stop_consuming ( )
inline

Stop consuming messages.

This shuts down the internal callback and discards any unread messages.

virtual void mqtt::client::subscribe ( const string &  topicFilter)
inlinevirtual

Subscribe to a topic, which may include wildcards using a QoS of 1.

Parameters
topicFilter
virtual void mqtt::client::subscribe ( const string &  topicFilter,
int  qos 
)
inlinevirtual

Subscribe to a topic, which may include wildcards.

Parameters
topicFilterA single topic to subscribe
qosThe QoS of the subscription
virtual void mqtt::client::subscribe ( const string_collection topicFilters)
virtual

Subscribes to a one or more topics, which may include wildcards using a QoS of 1.

Parameters
topicFiltersA set of topics to subscribe
virtual void mqtt::client::subscribe ( const string_collection topicFilters,
const qos_collection qos 
)
inlinevirtual

Subscribes to multiple topics, each of which may include wildcards.

Parameters
topicFiltersA collection of topics to subscribe
qosA collection of QoS for each topic
bool mqtt::client::try_consume_message ( const_message_ptr *  msg)
inline

Try to read the next message from the queue without blocking.

Parameters
msgPointer to the value to receive the message
Returns
true is a message was read, false if no message was available.
template<typename Rep , class Period >
bool mqtt::client::try_consume_message_for ( const_message_ptr *  msg,
const std::chrono::duration< Rep, Period > &  relTime 
)
inline

Waits a limited time for a message to arrive.

Parameters
msgPointer to the value to receive the message
relTimeThe maximum amount of time to wait for a message.
Returns
true if a message was read, false if a timeout occurred.
template<class Clock , class Duration >
bool mqtt::client::try_consume_message_until ( const_message_ptr *  msg,
const std::chrono::time_point< Clock, Duration > &  absTime 
)
inline

Waits until a specific time for a message to occur.

Parameters
msgPointer to the value to receive the message
absTimeThe time point to wait until, before timing out.
Returns
true if a message was read, false if a timeout occurred.
virtual void mqtt::client::unsubscribe ( const string &  topicFilter)
inlinevirtual

Requests the server unsubscribe the client from a topic.

Parameters
topicFilterA single topic to unsubscribe.
virtual void mqtt::client::unsubscribe ( const string_collection topicFilters)
inlinevirtual

Requests the server unsubscribe the client from one or more topics.

Parameters
topicFiltersA collection of topics to unsubscribe.

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