Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

200
Views
i want to use nestjs emitter from main.ts. i have function in controller class which has @OnEvent('send>email'). please guid me how can i invoke event
  1. Hi, i am new in nodejs please guid me. this is main.ts class which have statement to invoke OnEvent('send_email') in controller class you can see controller class also.
  2. i added it
       async function bootstrap() {
      const myEmitter = new EventEmitter2();
      const app = await NestFactory.createMicroservice(
        AppModule,
        appConfig.microservice,
      );
    
      //#region RabbitMQ Implementation
      const email = {
        email: {
          from: 'GeoNoon <xzy@yahoo.com>',
          to: 'ali.whu@yahoo.com',
          subject: 'Hello ✔',
          html: '<b>Hello world? 测试邮件</b>',
        },
      };
      amqplib.connect(
        'amqp://localhost',
        (error0, connection) => {
          if (error0) {
            throw error0;
          }
          connection.createChannel((error1, channel) => {
            if (error1) {
              throw error1;
            }
            const queue = 'Test_emails_queue';
            channel.assertQueue(queue, { durable: false });
            channel.sendToQueue(queue, Buffer.from(JSON.stringify(email)));
            console.log(' [*] Waiting for message', queue);
            // myEmitter.on('send_email', function(email:{}) {
            //   console.log(this.event, email);
            // });
            const response = myEmitter.emit('send_email', JSON.stringify(email));
            console.log(response);
          });
        },
      );
      //#endregion
    
      await app.listen();
      console.log('Geonoon Mail Microservice is listening');
    }
    bootstrap();
  1. this is app.controller class which has method to send email to customer.
  2. please help me how can i do this. thanks
       export class AppController {
  constructor(private readonly appService: AppService) {}

  @OnEvent('send_email')
  async handlerEmailSend(data: Record<string, any>) {
    console.log(data);
    if (
      data.email &&
      data.email.from &&
      data.email.to &&
      data.email.subject &&
      (data.email.html || data.email.text)
    ) {
      return await this.appService.sendEmail(data as EmailContent);
    } else {
      Logger.warn('电子邮件数据格式错误');
    }
  }
}
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!