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

pass disconnect properties to on_disconnect handler #151

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

miketrebilcock
Copy link

on_disconnect_handler does not pass through the parameters from paho. I need to check the rc parameter to understand the reason for disconnect.

@Sohaib90
Copy link
Collaborator

@miketrebilcock can you provide a little more context to this PR. Why is it required? What did you do to fix the issue? Which tests refer to the change you propose in this PR. Thanks :)

@miketrebilcock
Copy link
Author

I was having issues with connections where the server was forceable disconnecting the client. It was passing messages to indicate why the connection was being disconnected, by the library was passing them through to my code. This PR ensures that any data passed in the disconnect is accessible to code that is using the library.

I noticed that some extra commits have managed to be added to the PR, I'll update it to remove them.

@Sohaib90
Copy link
Collaborator

@miketrebilcock can you add some tests to ensure that the changes you have proposed actually work?

@howroyd
Copy link

howroyd commented Jan 16, 2024

Massive +1 to this PR please.

To add use case as mentioned:
The userdata passed into the callbacks in my case has condition variables (threading.Event) to signal an "is_connected" state (rather than using globals.) I can set this variable in on_connect because the userdata structure is passed through, but I cannot clear on_disconnect.

e.g.:

@dataclasses.dataclass
class Userdata:
   connected: mp.Event = dataclasses.field(default_factory=lambda: mp.Event())

@mqtt.on_connect()
def on_connect(client, userdata: Userdata, flags, rc):
    if 0 == rc:
        userdata.connected.set()

@mqtt.on_disconnect()
def on_disconnect(client, userdata: Userdata, rc):  # <- this doesn't work because the args aren't forwarded
    userdata.connected.clear()

@miketrebilcock
Copy link
Author

I'll try and get the tests added this week so that it can be merged.

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

Successfully merging this pull request may close these issues.

3 participants