IoT::MQTT

class MQTTClient

Library: IoT/MQTT
Package: MQTTClient
Header: IoT/MQTT/MQTTClient.h

Description

The interface for MQTT clients.

Implementations are expected to receive their client ID and server URI via an implementation defined configuration mechanism. Once configured, a MQTTClient always uses the same client ID and connects to the same server. A MQTT client should automatically attempt to reconnect if the connection to the server is lost.

Member Summary

Member Functions: connect, connectAsync, connected, connectionInfo, disconnect, id, publish, publishMessage, serverURI, statistics, subscribe, subscribeMany, subscribedTopics, unsubscribe, unsubscribeMany

Types

Ptr

typedef Poco::SharedPtr < MQTTClient > Ptr;

Constructors

MQTTClient

MQTTClient();

Creates the MQTTClient.

Destructor

~MQTTClient virtual

virtual ~MQTTClient();

Destroys the MQTTClient.

Member Functions

connect virtual

virtual ConnectionInfo connect() = 0;

Connects to the server if not already connected.

Normally, the client connects automatically when a message is published or a topic is subscribed to.

Returns a ConnectionInfo object containing information about the connection.

Fires the connected event if successful.

Throws a Poco::IOException if the connection cannot be established.

connectAsync virtual

virtual void connectAsync() = 0;

Connects to the server if not already connected.

Connecting will be done asynchronously in a background thread.

A successful connection will be reported by firing the connected event.

connected virtual

virtual bool connected() const = 0;

Returns true if the client is currently connected to the server.

connectionInfo virtual

virtual ConnectionInfo connectionInfo() const = 0;

Returns a ConnectionInfo structure describing the currently active connection. If not connected, the ConnectionInfo's serverURI will be empty.

disconnect virtual

virtual void disconnect(
    int timeout
) = 0;

Disconnects from the server.

In order to allow the client time to complete handling of messages that are in-flight when this function is called, a timeout period is specified (in milliseconds). When the timeout period has expired, the client disconnects even if there are still outstanding message acknowledgements. The next time the client connects to the same server, any QoS 1 or 2 messages which have not completed will be retried depending on the clean session settings for both the previous and the new connection.

id virtual

virtual const std::string & id() const = 0;

Returns the configured client ID.

publish virtual

virtual int publish(
    const std::string & topic,
    const std::string & payload,
    int qos
) = 0;

Publishes the given message on the given topic, using the given QoS.

Returns a delivery token which can be used with the messageDelivered event to verify that the message has been delivered.

Throws a Poco::IOException if the message cannot be published.

publishMessage virtual

virtual int publishMessage(
    const std::string & topic,
    const Message & message
) = 0;

Publishes the given message on the given topic.

Returns a delivery token which can be used with the messageDelivered event to verify that the message has been delivered.

Throws a Poco::IOException if the message cannot be published.

serverURI virtual

virtual const std::string & serverURI() const = 0;

Returns the configured server URI.

statistics virtual

virtual Statistics statistics() const = 0;

Returns statistics about published and received topics and message counts.

subscribe virtual

virtual void subscribe(
    const std::string & topic,
    int qos
) = 0;

This function attempts to subscribe the client to a single topic, which may contain wildcards. This call also specifies the Quality of service requested for the subscription.

Throws a Poco::IOException if there was a problem registering the subscription.

subscribeMany virtual

virtual void subscribeMany(
    const std::vector < TopicQoS > & topicsAndQoS
) = 0;

This function attempts to subscribe the client to a list of topics (with associated QoS levels), which may contain wildcards.

Throws a Poco::IOException if there was a problem registering the subscriptions.

subscribedTopics virtual

virtual std::vector < TopicQoS > subscribedTopics() const = 0;

Returns a vector containing all currently subscribed topics with their QoS level.

unsubscribe virtual

virtual void unsubscribe(
    const std::string & topic
) = 0;

This function attempts to remove an existing subscription made by the client.

Throws a Poco::IOException if there was a problem removing the subscription.

unsubscribeMany virtual

virtual void unsubscribeMany(
    const std::vector < std::string > & topics
) = 0;

This function attempts to remove existing subscriptions to a list of topics made by the specified client.

Throws a Poco::IOException if there was a problem removing the subscriptions.

Variables

connectionClosed

Poco::BasicEvent < void > connectionClosed;

Fired when the client has disconnected from the server.

connectionEstablished

Poco::BasicEvent < const ConnectionEstablishedEvent > connectionEstablished;

Fired when a connection to the MQTT server has been established.

connectionLost

Poco::BasicEvent < const ConnectionLostEvent > connectionLost;

Fired when the connection to the MQTT server has been lost.

messageArrived

Poco::BasicEvent < const MessageArrivedEvent > messageArrived;

Fired when a message has been received for one of the subscribed topics.

messageDelivered

Poco::BasicEvent < const MessageDeliveredEvent > messageDelivered;

Fired when a message has been delivered.

Securely control IoT edge devices from anywhere   Connect a Device