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

Is there a need to resubscribe to topics in async client clean_session(false) when disconnect event [-3] occurs. #507

Open
mayur-IG opened this issue Jul 26, 2024 · 2 comments

Comments

@mayur-IG
Copy link

mayur-IG commented Jul 26, 2024

While using Async Client [paho-mqttpp3 paho-mqtt3as] with automatic_reconnect example connection options below:

    connOpts = mqtt::connect_options_builder()
        .user_name(username)
        .password(password)
        .keep_alive_interval(std::chrono::seconds(KEEP_ALIVE_INTERVAL))
        .automatic_reconnect(std::chrono::seconds(AUTO_RECONNECT_MIN), std::chrono::seconds(AUTO_RECONNECT_MAX))
        .clean_session(false)
        .ssl(mqtt::ssl_options_builder().finalize())
        .finalize();

    try {
        cli.set_callback(*this);
        cli.connect(connOpts)->wait();
    } 

There are some cases when the client disconnects and then reconnects. the application can handle these drops, so we do not crash out or throw any exception.
The Issue is that we are able to publish messages but not receive any messages after the reconnection. I am not sure if there is a need to resubscribe to the topics again after this disconnect event [-3]

@mayur-IG mayur-IG changed the title Is there a need to resubscribe to topics in async client when disconnect event [-3] occurs. Is there a need to resubscribe to topics in async client clean_session(false) when disconnect event [-3] occurs. Jul 26, 2024
@mayur-IG
Copy link
Author

Noticed that the disconnect was longer than KEEP_ALIVE_INTERVAL. maybe this caused the session to clear out.

@fpagliughi
Copy link
Contributor

No, whether the session is still present has nothing to do with the KEEP_ALIVE_INTERVAL.

Since you are setting clean_session to false, you're telling the server to keep a persistent session for your client. Although you don't show the client construction, I assume that you're setting a unique Client ID, and reusing the same ID when you reconnect. The server tracks the session by Client ID.

With MQTT v5 there is a session expiry interval property that goes in the connect packet. This tells the server how long to keep the session after the client disconnects. You can tell it to keep the session active for an hour, day, week; whatever.

With MQTT v3 (which you appear to be using), the session is supposed to stay "forever". This was a mistake in the protocol design, because clients could connect, request a session, then disconnect and never be seen again. In which case the server accumulated all their messages forever. Because of this, many v3 servers have a configured expiry interval, after which they will discard the session.

In either case, when you connect, the server will tell you if it remembers your Client ID and has a "session present". You can get that from the server response to the connect call, as shown in a few examples, like:

// If there is no session present, then we need to subscribe, but if
// there is a session, then the server remembers us and our
// subscriptions.
if (!rsp.is_session_present())
cli.subscribe(TOPIC, QOS)->wait();

If the server does not have a session present for your client, then you do need to re-subscribe.

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