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

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


Back to the top