Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [mosquitto-dev] Callback on_message called multiple times when using MQTT v5.0
  • From: Aleksandar Nikolic <an010@xxxxxxxx>
  • Date: Mon, 6 Jun 2022 04:46:25 +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=5UjOgALG8DhQH+jVTL/APax/7uvHVlptU/mtI7wP81o=; b=P1ZukgCDZytCr+GL1jqlFK3ymvg9iQ9T4+Qnwn+SUo78VYSRaCxfofoEolE2pA8GWLo9IXQqckiUROIXz4kJ5ds5n9q+Niu0efHiadpFxhJwZqHcJxHKJDAme6fc6kJLAyLXN0TAjY2sInEZry/jB6iQfKBV5kbr7wWUZemUNYA21J7yDmfV+sa5aAJQ3aiD5WQwyekvNmky+GKdANHLNTS/81K+Wgq+yuRzI3M+b5YLIk06exOG1e/b4Njv+sS8xSpZW0qEwXAS5RP7Z4AxQB6oR5O2DsU/D2ZBO4dRXGdVLRJODwqzMLRI8eYHork/AyH20N6pSS9P1IqA/YVaHQ==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=mJPymPEege4oYhtiDooYmcjq1cVq0UGMHmTqbzei4ozdWJzVxcWJWzftgZvV/na9Bi2EtaVTBidX4RClr2MO8mpMsx1rMBKFpcoaRZPFe5qjB2TSJ7PtcvL4O/Pm2VFrvTRYlhlGPfC7WY1Lw0lc0l+v5FVvvyY4Yf8afIFF0CwE5d6/xbWUy2PPp/sDyjbYOeAy9oWpyE6E4ZEZdKFJRLe11YL8DmIHSWeEAzMxOhb+5uDotjPgduY2VxFP66VAriGFAlGnT6sqfat7zkMuzOA7DEsj3/ZIyTRKHZobt4hY+RSMCB7eJco8K3wA1w5/U8G+lfu6fmzWd0wnsDsGeA==
  • 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: 3ed4b315-9fa1-7bf4-8a11-8d665ab0353c
  • Thread-index: AQHYdzhO7wuHe4fQeEGgHoJdZ4D4H60+ONGAgAOW+Yw=
  • Thread-topic: [mosquitto-dev] Callback on_message called multiple times when using MQTT v5.0

Hi Roger,

okay, I understand why I get duplicated messages, I suspected that but its good to have an official confirmation. But is there a way to get only one message with MQTT v5.0? In other words, is there a way that my callback is called only once instead of multiple times (once per subscription). I have added `allow_duplicate_messages false` in my configuration, sadly no changes noticed.

Mit freundlichen Grüßen / Kind regards,

Aleksandar Nikolic

--

E-mail: an010@xxxxxxxx
Phone: +49 (0)176 57673542

From: mosquitto-dev <mosquitto-dev-bounces@xxxxxxxxxxx> on behalf of Roger Light <roger@xxxxxxxxxx>
Sent: Friday, June 3, 2022 11:48 PM
To: General development discussions for the mosquitto project <mosquitto-dev@xxxxxxxxxxx>
Subject: Re: [mosquitto-dev] Callback on_message called multiple times when using MQTT v5.0
 
Hi Aleksandar,

Back in the days of the MQTT v3 spec and change to v3.1 (around ten
years ago now) there were a few discussions about the idea that the
broker should only deliver a single message for clients with
overlapping subscriptions like you have. MQTT v5.0 does not have this
requirement which is why you receive the duplicate messages for each
of the subscriptions.

However, I see that the v3.1.1 spec also allows duplicate messages for
overlapping subscriptions. Also neither the v3.1 nor the v3 specs
mention overlapping subscriptions. So it seems as though it's unlikely
anywhere else restricts v3.x messages this way.

You can disable the restriction on duplicate messages by setting
`allow_duplicate messages true`, this will be the default in the next
version as well.

Cheers,

Roger

On Fri, 3 Jun 2022 at 11:54, Aleksandar Nikolic <an010@xxxxxxxx> wrote:
>
> 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
>
>
> _______________________________________________
> mosquitto-dev mailing list
> mosquitto-dev@xxxxxxxxxxx
> To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/mosquitto-dev
_______________________________________________
mosquitto-dev mailing list
mosquitto-dev@xxxxxxxxxxx
To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/mosquitto-dev

Back to the top