graphql-mqtt-subscriptions
graphql-mqtt-subscriptions copied to clipboard
Feature request: Add a way to return topic with message
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.
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.
Any progress on this?
I've implemented this feature in an up to date fork of this repository. If you would like, I can submit a merge request.
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?