-
Notifications
You must be signed in to change notification settings - Fork 51
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
add on_message callback with user data #181
Conversation
resolves #178 |
I think the root problem of accessing the user data object inside the on_message callback can be done with the current version of the library without any changes, although it may be desired to make a tweak to make public property in place of what is a private field today. The I verified this with a modified version of the simpletest where I used this for the message callback:
This seems to work successfully with the currently released version of the library. Can you try that within your project(s) to see if this solution is workable for you? This solution does rely on accessing However perhaps it would be worthwhile to change it so that the user_data isn't "private" with the leading underscore and instead is accessible at Please let me know if I've misunderstood the root problem or anything about it. If I do understand properly and if this solution works for you as it seems to for me I think I'm in favor of adopting only the name change of user_data property since it will provide the same functionality with slightly different syntax and keep the code less complex without the addition of a new type callback and add_callback function, and is inherently backwards compatible since the existing function signature can be used. |
Reaching inside the MQTT object sounds more like a workaround, esp. with the (sort of) private member access. My primary interest was to be able to pass the data from/to the message callback, however upon seeing that all the other callbacks (connect, disconnect, publish, subscribe, unsubscribe) are being passed the If this was not the arguably most used callback, I'd propose to bite the bullet and add the user_data argument rather than introduce more callback types, maybe as a keyword argument (which I am not sure would work seamlesslish). Also, the There is also the matter of documentation. The MQTT initializer Python doc says:
which does not match reality. The example code in the repo contains the usage matching current version of the library, however I am not sure how many peruse the examples for code construction. If the reach-inside-MQTT approach prevails, it should be definitely accompanied with some pronounced documentation/examples to avoid (not so frequent ?) MiniMQTT user callbacks (sic!) to determine how to pass the user data around. |
@FoamyGuy thank you much for investing so much time and thought in this. I am really grateful. I've started testing your suggestion and it certainly seems fine for our use case, although as you say, perhaps if it could "change ... so that the user_data isn't "private" with the leading underscore" that would certainly feel more 'tidy'. @vladak I apologise but I can't entirely understand if you are saying you don't accept this approach? Particularly if the 'offending' underscore were removed this would seem to be an entirely 'normal' bit of functionality for the module? I'm just glad either of you was willing to try to work on this :-) |
Removing the underscore and providing sufficient amount of documentation is definitely practical solution within given constraints (i.e. not to break pre-existing users, particularly those of CP) and likely better than supporting bunch of callbacks for the same event with different signatures because that solution does not solve the inconsistency, however still makes me cringe a little bit. I will try to revert my changes within this PR and see what it would take to provide appropriate documentation for the callbacks. |
Closing in favor of PR #188. |
This change introduces "on_message" callbacks with
user_data
. I chose to take a conservative approach in order not to break existing code, hence the new callback methods.