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

Separate callback for each subscribe call like Aedes.subscribe() #1951

Open
nolimitdev opened this issue Oct 25, 2024 · 3 comments
Open

Separate callback for each subscribe call like Aedes.subscribe() #1951

nolimitdev opened this issue Oct 25, 2024 · 3 comments

Comments

@nolimitdev
Copy link

nolimitdev commented Oct 25, 2024

Is your feature request related to a problem? Please describe.
I'm always frustrated when I subscribe to more topics and must find correct message in shared callback on.message using very long switch or if/elseif.

Describe the solution you'd like
I would like to have ability to call subscribe with one topic (can include + and #) and separate callback. Exactly like npm aedes has... Aedes has this feature and it is great because supports + and # https://github.com/moscajs/aedes/blob/HEAD/docs/Aedes.md#aedessubscribe-topic-deliverfunc-callback Im very missing something like this in npm mqtt package.

Maybe it could be easy implemented by looking for source code of aedes. Also Chat GPT suggest good looking idea how to compare subscribe pattern with received message without regexp and run appropriate subscription callback.

@robertsLando
Copy link
Member

I suggest you to implement this yourself by creating a wrapper around subscribe/unsubscribe functions using mqemitter library. I don't want to add another library to match topic patterns as this is quite easy to do for end users. Also this library should work both in browser and on nodejs

@nolimitdev
Copy link
Author

nolimitdev commented Oct 26, 2024

I have already created wrapper (same code for both node and browser) without any other package. Another library is not needed comparison seems to be easy...

 compare(pattern, topic) {

    const patternSegments = pattern.split('/'),
          topicSegments = topic.split('/');

    for (let i = 0; i < patternSegments.length; i++) {
        if (patternSegments[i] == '#')
            return true;

        if (patternSegments[i] != '+' && patternSegments[i] != topicSegments[i])
            return false;
    }

    return (patternSegments.length == topicSegments.length) ? true : false;

}

Btw: It would be great to document that after Client.end() subscriptions are remembered and will be reused when later is manually called Client.connect() or Client.reconnect().

@robertsLando
Copy link
Member

robertsLando commented Oct 28, 2024

From docs:

resubscribe : if connection is broken and reconnects, subscribed topics are automatically subscribed again (default true)

I have already created wrapper (same code for both node and browser) without any other package. Another library is not needed comparison seems to be easy...

Do you want to submit a PR? Remember to add unit tests as well

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

2 participants