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.

Sharing same @EventPattern across 2 handlers is broken if 2nd handler uses an interceptor

See original GitHub issue

Bug Report

Current behavior

Given two controller route handlers that subscribe to the same @EventPattern, both handlers are executed as expected. However, if an interceptor is used on the 2nd handler, the interceptor is executed but the handler itself is never executed

Input Code

Reproduce: https://github.com/RoxKilly/nestjs-redis-troubleshoot/tree/issue-7913

export class AppController implements OnModuleInit {
  constructor(@Inject('REDIS_CLIENT') private redis: ClientRedis) {}

  onModuleInit(): any {
    this.redis.emit(eventPattern, 'NEW EVENT');
  }

  @EventPattern(eventPattern)
  handler1(data) {
    Logger.log(data, 'Handler 1');
  }

  /** This will not be triggered unless we disable the interceptor or remove the other handler */
  @EventPattern(eventPattern)
  @UseInterceptors(LoggingInterceptor)
  handler2(data) {
    Logger.log(data, 'Handler 2');
  }
}

Without Interceptor

Notice that both handler1 and handler2 receive and log the NEW EVENT

Screenshot 2021-08-22 105656

With Interceptor on 2nd handler

Notice the interceptor for handler2 is triggered, but handler2 never executes (it doesn’t log the event)

Screenshot 2021-08-22 105408

Expected behavior

I expected both handlers to be triggered even if I use an interceptor on the 2nd handler.

Possible Solution

Removing the interceptor from the 2nd handler makes this go away but is not a practical workaround.

Environment


Nest version: 8.0.6

 
For Tooling issues:
- Node version: 16.4.0 
- Platform:  Windows

Others:

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
kamilmysliwieccommented, Sep 30, 2021

Fixed in 8.0.9

0reactions
davidgabrichidzecommented, Sep 9, 2021

@HackPoint never, so far. I mean, ... Event pattern: undefined never showed to me. When there is no listener, message always shows name of pattern.

Read more comments on GitHub >

github_iconTop Results From Across the Web

NestJS ClientRedis emits but EventPattern handler not triggered
NestJS was not listening on the Redis channel because after creating the microservice, I never started it. In main.ts I had to change:...
Read more >
Microservices | NestJS - A progressive Node.js framework
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines ...
Read more >
Domain Events – Take 2 - Udi Dahan
One thing that we want to keep in the solution is that all the code to define events, their names, and the parameters...
Read more >
Event Handler called twice with the same event - Google Groups
I'm facing some problems using TrackingProcessors. Seems that sometimes, some handlers are called twice. It happens expecially if the handler method is very ......
Read more >
Discovery and Service Mapping Patterns release notes
Read patterns documentation in one place. The ServiceNow Store documentation for IT Operations Management patterns is now in the release family sections of...
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