watermill
watermill copied to clipboard
Multiple topics, same handler
I have multiple topics and 1 generic handler that sends those messages to an external system.
For example:
Topics: user_topic, post_topic, comment_topic
Handler: send_to_segment_handler
The router currently has a check for this and returns an error. Am I designing this correctly, or is there a better way to do what I'm trying to accomplish?
Hey, can you describe this a little bit more in detail? Are the events coming from a single place and you need to route them onto these 3 topics?
i have something very similar, basically a single message that might require mutliple messages sent out to different topics.
So, single place (ex. change_email_request_handler injesting from topic.change_email_request) that outputs to two different topics
(ex. user_detail_changed topic and change_email_response)
I think what you want is consumer groups.
I'm going to write proper docs on this, but let me just paste what I shared on our discord server recently:
Use case 1
- You have 5 replicas of the same service, each with an event handler subscribing for topic
OrderPlaced. - You want a particular message to be delivered only to one of them to process it once.
- In that case, you would have all 5 event handlers with the same consumer group, for example
orders-service_order-completed-handler.
Use case 2
- You have one service with 5 event handlers.
- Each subscribes to
OrderPlacedand each does something different in reaction to it. - You want a particular message to be delivered to all of them.
- You would give each a unique consumer group, like
order-completed-handler,send-newsletter-handler,add-discount-handler, etc.
Use case 3
- Both cases combined. You have use case 2 but decided to run 5 replicas of your service.
- Now when a message comes in, you want to deliver it to all 5 handlers but only to one replica each.
- You end up with groups:
orders-service_order-completed-handler,orders-service_send-newsletter-handler,orders-service_add-discount-handler, etc.
Actually, I think I got it wrong, as you want to publish messages on two topics.
In this case, you can either:
- Have two handlers, each publishing one message on a different topic.
- Have one handler that calls a publisher directly. You could use
AddNoPublisherHandlerfor this on the router.