Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[mosquitto-dev] Callback on_message called multiple times when using MQTT v5.0
  • From: Aleksandar Nikolic <an010@xxxxxxxx>
  • Date: Fri, 3 Jun 2022 10:54:43 +0000
  • Accept-language: en-US
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=NiTFlsuWvPXAuPBeVCKoLPc6r/8d9x0wWZ1iLW/gH9k=; b=YqPTnjQWb8mqJwUiNec0mNwDvIYOp0hH7sXyAi3/nLDjDMYVmN7mlV55plGiMGXQEVDF/iqmkvSXmHm5H5NK0YSEmrNudm3KtPRY1EkGRsA7xzOWtEvlIhCLHWigYlIakWiAwIj+r2sEi8w5Q/R8QbI2eVa7BXHC0CfJxCMzwMLa54iv+UsJOLM2yqEURu7LpvnrxUEigArgBkLlABp82lgKMfgUd1m+Od4zNd0uuTrAv8jKKozyK9puiQOPt1LgYa7FbEjAqhhSXw8ZeYLENNRHEKEiako9NP+Y7KA8hU8nwlL5U1FAbml0brZBawAJ2b/XvcXTtFk3pJ+C3KJ5Tw==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Ki7rzK11bprK5xTxyDQd9pQpchQhnIHBgeyRgXhMjTSqIT4Db5KAlHZpJvR84vh8BWCJTo0afV1ICUnKukNjAuxFp6IgLdBZCkglb2PTRV0+BoUS6yRd4hCmSe1IrO6u4R6k6jIqmjSnWhbnKuxlccitpEc4dbPd09fbs3SgWcV/1+UWiCPq7nR9oGBCmRjBumcgsvUMs94uOXWQEbPzKDNvjddHywtxsoMTnsSjbwfF0IDYOIyTHK4Vx8ANcKYL1MhC3T9kVORyi3g1//Vtk6zkyEZGElSOaJCnvkWtvkfvDDA7jeXpUDKKn7WQF2onlEs6ARa+ji+/ta/ng/mAHg==
  • Delivered-to: mosquitto-dev@xxxxxxxxxxx
  • List-archive: <https://www.eclipse.org/mailman/private/mosquitto-dev/>
  • List-help: <mailto:mosquitto-dev-request@eclipse.org?subject=help>
  • List-subscribe: <https://www.eclipse.org/mailman/listinfo/mosquitto-dev>, <mailto:mosquitto-dev-request@eclipse.org?subject=subscribe>
  • List-unsubscribe: <https://www.eclipse.org/mailman/options/mosquitto-dev>, <mailto:mosquitto-dev-request@eclipse.org?subject=unsubscribe>
  • Suggested_attachment_session_id: a1f0578a-2ec8-8e9f-1a6a-a0a5837bef38
  • Thread-index: AQHYdzhO7wuHe4fQeEGgHoJdZ4D4Hw==
  • Thread-topic: Callback on_message called multiple times when using MQTT v5.0

Hello,


why is the on_message function called 3 times when I am using MQTT v5.0, and exactly once when I am not using MQTT v5.0? The mosquitto version is 2.0.14. I have also debugged a little and the message is really published multiple times when I use the MQTT v5.0. Why is that?


Here is the code:


```

#include <mosquitto.h>
#include <mqtt_protocol.h>
#include <thread>
#include <cstring>

#define SERVER_HOST "127.0.0.1"
#define SERVER_PORT 1883

/* Callback called when the client receives a message. */
static void on_message(struct mosquitto* mosq, void* obj, const struct mosquitto_message* msg, const mosquitto_property* props)
{
    printf("%s called with topic %s\n", __FUNCTION__, msg->topic);
    return;
}

int main(void)
{
    struct mosquitto* mosq_;
    char msg[] = "testmsg";
    int qos = 0;

    mosquitto_lib_init();
    mosq_ = mosquitto_new("tx", false, NULL);
    if (mosq_ == NULL)
        return 1;

    // Commenting this function call results in the `on_message` being called exactly once.
    // With this line it is called 3 times (for each mosquitto_subscribe).
    mosquitto_int_option(mosq_, MOSQ_OPT_PROTOCOL_VERSION, MQTT_PROTOCOL_V5);

    mosquitto_message_v5_callback_set(mosq_, on_message);
    mosquitto_connect(mosq_, SERVER_HOST, SERVER_PORT, 60);
    mosquitto_loop_start(mosq_);
    mosquitto_subscribe(mosq_, NULL, "#", qos);
    mosquitto_subscribe(mosq_, NULL, "1/#", qos);
    mosquitto_subscribe(mosq_, NULL, "1/2/#", qos);

    mosquitto_publish_v5(mosq_, NULL, "1/2/3", strlen(msg), static_cast<void*>(msg), qos, false, NULL);
    std::this_thread::sleep_for(std::chrono::seconds(1));

    mosquitto_disconnect(mosq_);
    mosquitto_destroy(mosq_);
    mosquitto_lib_cleanup();

    return 0;
}
```

Regards



Back to the top