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

Feature request: Add a way to return topic with message #15

Open
henhen724 opened this issue Jul 13, 2020 · 4 comments
Open

Feature request: Add a way to return topic with message #15

henhen724 opened this issue Jul 13, 2020 · 4 comments

Comments

@henhen724
Copy link

When subscribing to wildcards a way to identify the particular topic which sent the message would be useful. If the asyncIterator returned two arguments message, topic, you could implement pubSubs that use this.

@henhen724
Copy link
Author

henhen724 commented Jul 18, 2020

private onMessage(topic: string, message: Buffer) {
    const subscribers = [].concat(
        ...Object.keys(this.subsRefsMap)
        .filter((key) => MQTTPubSub.matches(key, topic))
        .map((key) => this.subsRefsMap[key]),
    );

    // Don't work for nothing..
    if (!subscribers || !subscribers.length) {
      return;
    }
    const messageString = message.toString(this.parseMessageWithEncoding);
    let parsedMessage;
    try {
      parsedMessage = JSON.parse(messageString);
      parsedMessage = { ...parsedMessage, topic };
    } catch (e) {
      parsedMessage = messageString;
    }

    for (const subId of subscribers) {
      const listener = this.subscriptionMap[subId][1];
      listener(parsedMessage);
    }
  }

This isn't a perfect fix but it adds the feature for people using JSON MQTT packets

The issue is to add this really you want to return {message: parsedMessage, topic} to the listener, but that would break all existing projects using this package.

@henhen724
Copy link
Author

Any progress on this?

@henhen724
Copy link
Author

I've implemented this feature in an up to date fork of this repository. If you would like, I can submit a merge request.

@henhen724
Copy link
Author

Hey will this pull request ever be added to main branch. It passes all tests, adds a useful feature, and has been idle for over a year?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants