nest: It is not possible to have multiple handlers `@EventPattern()` for the same event

Bug 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

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 9
  • Comments: 17 (7 by maintainers)

Most upvoted comments

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):

A single message can be subscribed to (and received) by multiple subscribers.

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…

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.

@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

Multiple event handlers is still calling only ONE handler in case if a global interceptor presence. I’ve made sure next.handle() is called event with zero logic in the interceptor and still got the same behaviour. Of course if interceptor is removed everything works find and all event pattern handlers are called. Also it works if subscribed manually next.handle().subscribe(...) but that’s very smelly workaround.

I created an issue for that https://github.com/nestjs/nest/issues/10184

Multiple event handlers is still calling only ONE handler in case if a global interceptor presence. I’ve made sure next.handle() is called event with zero logic in the interceptor and still got the same behaviour. Of course if interceptor is removed everything works find and all event pattern handlers are called. Also it works if subscribed manually next.handle().subscribe(...) but that’s very smelly workaround.