Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting SESSION_EXPIRY_INTERVAL in paho cpp V5 as client and mosquitto as server #323

Closed
greenGitCode opened this issue Jan 31, 2021 · 3 comments

Comments

@greenGitCode
Copy link

Hi,

I have tried to connect Paho cpp V5 as a client to mosquitto server 2.0.2.
Since the client will be disconnected and reconnected always, the server should keep all messages when the client is not present.
Everything works perfectly when I set the version to 3. I can publish and subscribe to any topics, and after reconnection, the client gets all messages successfully.

When I set the version to 5, publishing and subscribing work fine. But the server doesn't keep the messages when the client is not present. It looks like that the SESSION_EXPIRY_INTERVAL parameter is not set correctly.

    mqtt::properties connectProperties{
        {mqtt::property::SESSION_EXPIRY_INTERVAL, 999999999}
    };

    auto connOpts = mqtt::connect_options_builder()
            .mqtt_version(MQTTVERSION_5)
            .properties(connectProperties)
            .automatic_reconnect(std::chrono::milliseconds(lMinReconnect), std::chrono::milliseconds(lMaxReconnect))
            .keep_alive_interval(std::chrono::seconds(20000))
            .clean_session(false)
            .clean_start((bool) lSubCleanStart)
            .finalize();

    mqtt::client client(sServerAddress, sClientID, mqtt::create_options(MQTTVERSION_5));

    mqtt::callback::UserCallback cb;
    client.set_callback(cb);

    mqtt::connect_response rsp = client.connect(connOpts);

    mqtt::const_message_ptr msg;

    mqtt::subscribe_options subOpts((bool) lSubOwnPublish, true, (mqtt::subscribe_options::RetainHandling) lSubRetainHandling);

    mqtt::properties subProperties{
          {mqtt::property::SUBSCRIPTION_IDENTIFIER, 1}
    };

   client.subscribe(sSubTopic, lSubQOS, subOpts, subProperties);

   client.try_consume_message_for(&msg, std::chrono::milliseconds(lSubTimeout));

   std::string sTopic = msg.get_topic();
   std::string sMessage = msg.to_string();

I am doing something wrong? Did anyone have the same issue?
Any suggestion would be appreciated.

@fpagliughi
Copy link
Contributor

I just tried it out. I added a session expiry to the v5 "chat" sample, mqttpp_chat, using a 1 week (604800 sec) expiration interval:

https://github.com/eclipse/paho.mqtt.cpp/blob/4c1f545b5c33812320665fd4030039e0b26eac0a/src/samples/mqttpp_chat.cpp#L77-L88

I ran the sample and checked the connect packet it sent using WireShark. It shows the properly-formed packet with a session expiry property of 604800.

So, on the client side, everything looks good. If the broker is not handling it properly, that's beyond what I'm able to do in the library here. Perhaps bring the issue to Mosquitto:
https://github.com/eclipse/mosquitto/issues

@fpagliughi
Copy link
Contributor

fpagliughi commented Jan 31, 2021

I should mention that WireShark is an excellent debug tool for things like this. It understands MQTT, including v5 and has various helpful filters including simply "mqtt" to just display the MQTT traffic on a specific interface.

Also, side point, but with the connect options, if you specify v5, you don't need to manually set clean session to false. That is done automatically when you set v5.

@greenGitCode
Copy link
Author

I should mention that WireShark is an excellent debug tool for things like this. It understands MQTT, including v5 and has various helpful filters including simply "mqtt" to just display the MQTT traffic on a specific interface.

Also, side point, but with the connect options, if you specify v5, you don't need to manually set clean session to false. That is done automatically when you set v5.

Thanks for the quick reply. I will first try it with Wireshark. Then I will write the issue on the mosquitto page.

emrahayanoglu pushed a commit to emrahayanoglu/paho.mqtt.cpp that referenced this issue May 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants