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

I wanna Add addition params [topicFilter] on Event of client.on("messsage",callback) #1846

Closed
LZW-Andrewlu opened this issue Apr 19, 2024 · 3 comments

Comments

@LZW-Andrewlu
Copy link

Is your feature request related to a problem? Please describe.
When I subscribe to a topic with wildcard characters, I want to distinguish which topicFilter it is in the message event and distribute it to the corresponding callback function, and the real topic field in the message event needs a matching process with the wildcard theme.

Describe the solution you'd like
Option 1: I hope the client can provide an API to detect whether a wildcard topic matches the actual topic in the message event callback;
Option 2: Add a parameter to onMessageCallback to indicate which subscription topic matches the current topic. This way, I can execute different event callbacks for different subscription topics in the message event,thus not match them manually with if...else...

Describe alternatives you've considered

Additional context
i want sub function like code below. and each callback only work with absolute topic&topicFilter

        sub(topic: string, callback: OnMessageCallback) {
            if (this.inst) {
                this.inst.subscribe(topic);
            }
            this.subTopics[topic] = new WeakRef(callback);
        },
@LZW-Andrewlu
Copy link
Author

Try to implement this function as below:

public static isMatched(topicFilter: string, topicName: string): boolean {
        let topicPos = 0;
        let filterPos = 0;
        let topicLen = topicName.length;
        let filterLen = topicFilter.length;
        if (topicFilter === topicName) {
            return true;
        } else {
            while (filterPos < filterLen && topicPos < topicLen) {
                if (topicFilter.charAt(filterPos) == "#") {
                    topicPos = topicLen;
                    filterPos = filterLen;
                    break;
                }
                if (
                    (topicName.charAt(topicPos) == "/" && topicFilter.charAt(filterPos) != "/") ||
                    (topicFilter.charAt(filterPos) != "+" &&
                        topicFilter.charAt(filterPos) != "#" &&
                        topicFilter.charAt(filterPos) != topicName.charAt(topicPos))
                ) {
                    break;
                }

                if (topicFilter.charAt(filterPos) == "+") {
                    for (let nextpos = topicPos + 1; nextpos < topicLen && topicName.charAt(nextpos) != "/"; nextpos = topicPos + 1) {
                        ++topicPos;
                    }
                }
                ++filterPos;
                ++topicPos;
            }
            if (topicPos == topicLen && filterPos == filterLen) {
                return true;
            } else {
                if (topicFilter.length - filterPos > 0 && topicPos == topicLen) {
                    if (topicName.charAt(topicPos - 1) == "/" && topicFilter.charAt(filterPos) == "#") {
                        return true;
                    }
                    if (topicFilter.length - filterPos > 1 && topicFilter.substring(filterPos, filterPos + 2) == "/#") {
                        return true;
                    }
                }
                return false;
            }
        }
    }

@robertsLando
Copy link
Member

Feel free to submit a PR

@woifes
Copy link

woifes commented Feb 5, 2025

I dont think this matcher functionallity should be implemented in this library because it would get bloated if to many use case specific logic is packed in here. But feel free to create a facade on this library for your needs. As inspiration you can checkout my facade in my monorepo
This second matcher function on the other hand could indeed part of this library. For example paho-mqtt in the python world also ships with such a function. Here is mine and this is the one from paho-mqtt

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

3 participants