Skip to content

Add message filtering ability #157

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

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

Add message filtering ability #157

wants to merge 1 commit into from

Conversation

thebigpotatoe
Copy link

Been using this library for a while and absolutely love it. The async nature is second to none above similar libraries. However I find myself writing boiler plate every time I set up a new project to filter incoming messages to achieve the correct action in the onMessage() callback.

To help improve on the library, I added the ability to filter the topic of an incoming message in the _onMessage() function when looping through each of the added callbacks from the user. This is achieved by supplying a chosen topic when adding a callback with onMessage() and a new method onFilteredMessage(). These achieve the same outcome, but one is easier to follow when reading code.

AsyncMqttClient& AsyncMqttClient::onMessage(AsyncMqttClientInternals::OnMessageUserCallback callback, const char* _userTopic = "#")' 'AsyncMqttClient& AsyncMqttClient::onFilteredMessage(AsyncMqttClientInternals::OnMessageUserCallback callback, const char* _userTopic)

I also added a shortcut for this by overloading subscribe(). By supplying a callback when adding a subscription, the callback will be added to the vector called by _onMessage() actually removing the need to supply a callback using onMessage().

uint16_t AsyncMqttClient::subscribe(const char* topic, uint8_t qos, AsyncMqttClientInternals::OnMessageUserCallback callback)

I think with these two methods and a little documentation on how to use them would make using the library a little more straightforward and far more efficient for new users when subscribing to more than one topic needing more than one callback. It will also reduce a bunch of boilerplate in a fair few of my projects and I hope a few from others.

I have tried to keep to the same formatting and style for easy integration, and have also updated all supporting files such as the examples and keywords.

New methods include;
- overloaded onMessage() to accept callback for given subscription
- onFilteredMessage()  to accept callback for given subscription
- overloaded subscribe() to add a callback to a specific subscription when subscribing
@NichtEuler
Copy link

This would be great instead of having to compare 5000 strings.

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.

2 participants