diff --git a/apps/api/src/api.ts b/apps/api/src/api.ts index cfaeafa00..6f0c8fba5 100644 --- a/apps/api/src/api.ts +++ b/apps/api/src/api.ts @@ -138,32 +138,30 @@ mqtt.on("message", (messageTopic, message) => { parsedMessage = JSON.parse(parsedMessage); } catch (err) { } - if(subscriptions[topic]) { - // Handle wildcard subscriptions - const validSubscriptions = Object.keys(subscriptions).filter(subscription => { - const subscriptionParts = subscription.split('/'); - const topicParts = topic.split('/'); + // Handle wildcard subscriptions + const validSubscriptions = Object.keys(subscriptions).filter(subscription => { + const subscriptionParts = subscription.split('/'); + const topicParts = topic.split('/'); - for (let i = 0; i < subscriptionParts.length; i++) { - if (subscriptionParts[i] === '#') { - return true; - } + for (let i = 0; i < subscriptionParts.length; i++) { + if (subscriptionParts[i] === '#') { + return true; + } - if (subscriptionParts[i] !== '+' && subscriptionParts[i] !== topicParts[i]) { - return false; - } + if (subscriptionParts[i] !== '+' && subscriptionParts[i] !== topicParts[i]) { + return false; } - return true; - }); + } + return true; + }); - validSubscriptions.forEach(subscription => { - - // console.log('SUUUUUUUUUUUUUUUUUUUUUB: ', subscriptions[subscription]) - Object.keys(subscriptions[subscription]).forEach(subscriptionId => { - subscriptions[subscription][subscriptionId](parsedMessage, topic); - }); + validSubscriptions.forEach(subscription => { + + // console.log('SUUUUUUUUUUUUUUUUUUUUUB: ', subscriptions[subscription]) + Object.keys(subscriptions[subscription]).forEach(subscriptionId => { + subscriptions[subscription][subscriptionId](parsedMessage, topic); }); - } + }); }); export default app diff --git a/packages/protolib/bundles/events/api/index.ts b/packages/protolib/bundles/events/api/index.ts index 5b647d2eb..3c864a448 100644 --- a/packages/protolib/bundles/events/api/index.ts +++ b/packages/protolib/bundles/events/api/index.ts @@ -4,16 +4,15 @@ import { generateEvent } from "../eventsLibrary" export const onEvent = (context, cb, path?, from?) => { context.topicSub('notifications/event/create/#', (async (msg: string, topic: string) => { try { - const message = JSON.parse(msg) - if (message) { - if (path && message['path'] != path) { + if (msg) { + if (path && msg['path'] != path) { return } - if (from && message['from'] != from) { + if (from && msg['from'] != from) { return } } - cb(message) + cb(msg) } catch (e) { console.error('Error parsing message from mqtt: ', e) }