graphql-mqtt-subscriptions icon indicating copy to clipboard operation
graphql-mqtt-subscriptions copied to clipboard

Feature request: Add a way to return topic with message

Open henhen724 opened this issue 5 years ago • 4 comments

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 avatar Jul 13 '20 00:07 henhen724

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 avatar Jul 18 '20 04:07 henhen724

Any progress on this?

henhen724 avatar Jul 31 '20 17:07 henhen724

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 avatar Feb 17 '21 05:02 henhen724

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?

henhen724 avatar Dec 28 '22 22:12 henhen724