class NRPMQTTClient

Overview

#include <nrp_mqtt_client.h>

class NRPMQTTClient {
public:
    // construction

    NRPMQTTClient(nlohmann::json clientParams);
    NRPMQTTClient();

    // methods

    virtual void subscribe(
        const std::string& address,
        const std::function<void(const std::string&)>& callback
    );

    virtual void publish(
        const std::string& address,
        const std::string& msg,
        bool retained = false
    );

    void publishDirect(const std::string& address, const std::string& msg);
    virtual void disconnect();
    virtual bool isConnected();
    virtual void clearRetained();
};

Detailed Documentation

Construction

NRPMQTTClient(nlohmann::json clientParams)

Constructor

Methods

virtual void subscribe(
    const std::string& address,
    const std::function<void(const std::string&)>& callback
)

Subscribe to MQTT topic ‘address’ with callback function ‘callback’.

virtual void publish(
    const std::string& address,
    const std::string& msg,
    bool retained = false
)

Publishes ‘msg’ to MQTT topic ‘address’.

void publishDirect(const std::string& address, const std::string& msg)

Publishes ‘msg’ directly to subscriber callbacks, without going through the MQTT broker. Only for testing.

virtual void disconnect()

Disconnects client from MQTT Broker.

virtual bool isConnected()

Check connection status to broker.

virtual void clearRetained()

Clear all topics with retain messages by sending an empty msg.