QMqtt5 extracting response topic from received message
-
Hello. In QMqtt5 some updates were introduced. One of those updates is response topic and correlation data.
Whenever server receives a message on a subscribed topic, there should be a way to extract its response topic (if the message has one) and then publish an acknowledgment message on that response topic.
I have read simmilar QT forum thread regarding simmillar topic:
https://forum.qt.io/topic/104756/mqtt-v5-request-response-example/7In QT documentation, I have found the following:
https://doc.qt.io/qt-6/qmqttpublishproperties.htmlOne of the public functions that this class contains is the following:
I am not fully understanding how can I use this class in my code. How can I retrieve what is the response topic?
In my code, I have MQTT_message_handler that is being called everytime a message that I am subscribed to is received:
void MainWindow::Mqtt_message_handler(QString topic,QString message){ qDebug("MQTT handler"); qDebug("Topic received = %s ",topic.toStdString().c_str()); qDebug("Message received = %s ",message.toStdString().c_str()); QMqttPublishProperties test; qDebug("response topic = %s",test.responseTopic().toStdString().c_str()); qDebug("corellation data = %u",test.correlationData().toUInt()); qDebug("\n"); heck if subscribed topic has a response topic auto response_topic = QMqttPublishProperties::ResponseTopic(); //LAST WILL RECEIVED if(strcmp(topic.toStdString().c_str(),LAST_WILL_T.toStdString().c_str())==0){ ui->cell1_edit->setText(message); } //TEST TOPIC RECEIVED if(strcmp(topic.toStdString().c_str(),TEST_T.toStdString().c_str())==0){ qDebug("Test topic message received"); } }
I am then monitoring the application output. I can see the following being printed when I receive a message that I am subscribed to:
MQTT handler Topic received = /topic/led Message received = Enable response topic = corellation data = 0
For some reason the response topic is empty and corellation data is 0. However, on my mqtt client device I have set the following publish properties:
static esp_mqtt5_publish_property_config_t publish_property_toggle_led = { .payload_format_indicator = 1, .message_expiry_interval = 1000, .topic_alias = 0, .response_topic = "/topic/test/response", .correlation_data = "5", .correlation_data_len = 1, };
I would very much appreciate if someone could clarify to me how to correctly retrieve the response topic.