It is not possible to have multiple handlers `@EventPattern()` for the same event
See original GitHub issueBug Report
It is not possible to have multiple handlers @EventPattern()
for the same event
Current behavior
Creating multiple handlers for the same event does not call all of them
Input Code
@EventPattern('notify')
eventHandler(data: any) {
KafkaController.IS_NOTIFIED = data.value.notify;
}
@EventPattern('notify')
secondEventHandler(data: any) {
KafkaController.IS_NOTIFIED_TWICE = data.value.notify;
}
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 a UsersController
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
Issue Analytics
- State:
- Created 4 years ago
- Reactions:8
- Comments:17 (7 by maintainers)
Top Results From Across the Web
EventPattern handler is not being triggered outside controllers
I've tried defining eventhandlers on multiple classes in my code, but none of them are triggered when an event is emitted to Redis...
Read more >interface EventPattern · AWS CDK
For a pattern to match an event, the event must contain all the field names listed in the pattern. The field names must...
Read more >Microservices | NestJS - A progressive Node.js framework
Wherever possible, Nest abstracts implementation details so that the same components ... To create an event handler, we use the @EventPattern() decorator, ...
Read more >fromEventPattern - RxJS
Note that if event handler was called with more than one argument, second and following arguments will not appear in the Observable.
Read more >Standard .NET event patterns | Microsoft Learn
The event handlers do not return a value, so you need to communicate that in another way. The standard event pattern uses the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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) . Instead of having a god controller, any controller/feature interested can subscribe to the event and do their part of work.
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.
@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…