question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:8
  • Comments:17 (7 by maintainers)

github_iconTop GitHub Comments

11reactions
alfredoperezcommented, Sep 30, 2019

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.

3reactions
kruegernetcommented, Dec 10, 2020

@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…

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found