New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
It is not possible to have multiple handlers @EventPattern()
for the same event
#3074
Comments
I am familiar with the code and I can work on a fix. I want to know if you consider this a bug or is working as intended. I came across this problem when working on an implementation of a service and we have the need to have multiple handlers per event. If this doesn't get fixed I will have to roll my own implementation for Kafka. |
It actually is intended. Wouldn't it be easier to have a one single entry point for your event and multiple services that are being used inside a controller? |
Most of the times it should be with a single entry point, but what about situations where an event can affect a set of features(modules) . It becomes easier to separate once that feature becomes it's own service if it grows to that. Also, since it is an event I think it make sense to have any number of subscribers/handlers. |
So that may cause issues for the request-scoped controllers. Basically, since every event will have a different "entry point" (different controller's handler), for each controller different request-scoped providers tree will be created. Assuming that there is a request-scoped provider used by both controllers (from 2 features subscribing to the event), each controller will get an exclusive instance of this provider (it would be impossible to share request-scoped context). Static providers would work fine though :) Either way, if you think that might be useful for you, I'm guessing it might be useful for other people as well. Feel free to create a PR, I'd love to take a look & review and hopefully merge shortly :) |
@kamilmysliwiec @alfredoperez Is there any work being done on this? We'll need support for multiple event handlers for the same event for our kafka implementation as well. Happy to contribute if needed. |
@theabuitendyk I used to have a fix for this, but I don't have it at the moment. I will look for it and get back to you |
@alfredoperez I'm looking at how I can add support for this to nest as we're on a tight schedule. I'll make a PR if/when I have a working solution, but would still like to see what you have. Any luck tracking it down? |
@alfredoperez You can disregard my last comment. :) I have a working solution now, although it's one that doesn't involve the same @EventPattern being used twice in the same app. |
Has there been any progress on this? As @alfredoperez mentioned it would be useful to have multiple event handlers for the same event in separate feature modules, with each hander only taking care of calling the services that concern that particular module. This could also be an option to be turned on/off either globally or at the module/controller level. |
@kamilmysliwiec, respectfully, this sounds like an edge case implementation challenge blocking the adoption of an expected behavior. If the architecture can't support multiple consumers of an event, you're not 'publishing' the event, you're sending a message (even if you don't wait for a reply). In other words, if you can't have multiple subscribers, the word 'publish' does not apply, and you don't actually have an Event Bus, so it will increases issues of excessive coupling as well as issues of circular dependency when trying to work around this shortfall. |
@kamilmysliwiec, it is actually stated in the docs (Microservices/Redis):
It appears that in the code that handleEvent() is called for both message patterns and event patterns, so isn't the documentation actually incorrect here? After the first handler is registered, any other handler for the same pattern added via server.addHandler() just overwrites the last callback, so in fact only the last handler for a given pattern is the one that handles it... |
Subscriber = Redis subscriber (separate application instance) in this context. |
Hi. Any update on this issue? |
Let's track this here #6334 |
Multiple event handlers is still calling only ONE handler in case if a global interceptor presence. |
Hello : |
I created an issue for that #10184 |
Bug Report
It is not possible to have multiple handlers
@EventPattern()
for the same eventCurrent behavior
Creating multiple handlers for the same event does not call all of them
Input Code
Expected behavior
Both event handlers should be called.
This is needed because an application can have different features/area that has to do some work after an event happen. Eg. We deleting a customer in a shopping class we could have an
OrderController
that will react to"delete-customer"
event and archive orders. Also, we could have aUsersController
that will delete user data.Possible Solution
There is a part of the code in NestJS that finds the handler and returns a single element. That can be changed to return all the handlers matching the event.
Environment
Nest version: 6.7
The text was updated successfully, but these errors were encountered: