async-mqtt-client icon indicating copy to clipboard operation
async-mqtt-client copied to clipboard

Add message filtering ability

Open thebigpotatoe opened this issue 4 years ago • 1 comments

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.

thebigpotatoe avatar Sep 06 '19 12:09 thebigpotatoe

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

NichtEuler avatar Oct 13 '19 21:10 NichtEuler