N
Novu10mo ago
Pawan Jain

Create topic inline with trigger similar to subscribers

Hi everyone, I have a question related to how to use topics. Seems like if I want to trigger an event and add a topic key that doesn't exist, then it errors out. What do you do for the use case where you don't know if there is a topic key ahead of the time? I could call the get topic by key endpoint but that seems too much work everytime I want to trigger an event. For example, I have some products that are out of stock and just now is back in stock. I would like to notify anyone who subscribes to the product back in stock event. But I won't know if anyone has subscribed before. So if my service generates a topic key "back-in-stock-product-id-1234", and that topic doesn't exist because no one has subscribed to it in a separate UI, then the trigger call fails. For another more complex case, I would like to notify anyone who subscirbes to back in stock notification for the product level but also store level. Ideally my service would generate 2 topic keys "back-in-stock-product-id-1234" and "back-in-stock-store-345", and then trigger the event. But like this case, as there are more and more types of topics, my service has to call the API endpoint to check if topic exist for every key, which seems like not performant. by @yellowbird_33747 Original Message Link
4 Replies
Pawan Jain
Pawan Jain10mo ago
Hi @yellowbird_33747 Thanks for sharing your usecase in detail. Since topic is just a group of subscribers. if you trigger to a non existing topic, you are getting error. If we allow topic creation inline, event will be triggered to an empty topic with no subscribers. When you will add subscribers to that topic? or you also want to add subscribers to topic inline?
--
--10mo ago
When adding subscribers to a topic, if the topic doesn't exist, is being created on-the-fly: https://github.com/novuhq/novu/blob/83b0579c9f26bf8732cd36f530b54fef9cc586e6/apps/api/src/app/topics/use-cases/add-subscribers/add-subscribers.use-case.ts#L34 So for this user's second flow I'd suggest to do: - Add subscribers to a topic as the topic if it doesn't exist won't be created. - Trigger event to a topic. For the first flow the user would need to check if the Topic has subscribers by fetching. Unfortunately we can not guess if the topic has subscribers magically without erroring during the triggering of the event. I would suggest the user to rethink their implementation considering that when adding subscribers to a topic there is the confirmation the topic will exist 100%.
yellowbird_33747
yellowbird_3374710mo ago
@Pawan Jain Adding empty topic inline when triggering a workflow sounds like a good option @pablo.fernandez.otero thanks for the confirmation. Seems like the only way right now is calling the search topic endpoint to make sure topic exists before passing it in to a trigger. So my use case, for every workflow trigger, I need to send to 3 topics, which also means I also need to call the search topic endpoint 3 times to validate if the topic exists. Do you have any performance concern related to this implementation?
--
--10mo ago
We considered that is overloading the workflow trigger with an extra job that is not its responsibility and the trigger already handles a lot of complex logic. At any time in any use case a call to either validate that a topic has subscribers assigned needs to happen. Either before triggering the event or during the runtime of triggering the event. We considered it was better to have users to do their checks before triggering an event and let them decide what to do, if trigger the event for a topic or not. This would introduce a bigger complexity to handle as we allow to trigger an event with mixed recipients. For example to different topics and different subscribers not related to those topics in the same trigger request. There could happen extreme complex cases where you would try to send a trigger to a topic not created with subscribers not still in the database. A lot of checks to do causing more computation time and increasing the average response time of the endpoint.