Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@golevelup/nestjs-rabbitmq- error when publish message: Channel closed #701

Open
dansantoss opened this issue Feb 23, 2024 · 0 comments
Open

Comments

@dansantoss
Copy link

I have an API and after updating my project, where when I publish a message to a RabbitMQ server the following error is returned:

Error: channel closed
    at ConfirmChannel.<anonymous> (/home/daniel/labs/projetos/ned-flanders-api/node_modules/@golevelup/nestjs-rabbitmq/node_modules/amqplib/lib/channel.js:39:18)
    at ConfirmChannel.emit (node:events:525:35)
    at ConfirmChannel.C.toClosed (/home/daniel/labs/projetos/ned-flanders-api/node_modules/@golevelup/nestjs-rabbitmq/node_modules/amqplib/lib/channel.js:175:8)
    at Connection.C._closeChannels (/home/daniel/labs/projetos/ned-flanders-api/node_modules/@golevelup/nestjs-rabbitmq/node_modules/amqplib/lib/connection.js:394:18)
    at Connection.C.toClosed (/home/daniel/labs/projetos/ned-flanders-api/node_modules/@golevelup/nestjs-rabbitmq/node_modules/amqplib/lib/connection.js:401:8)
    at Connection.C.onSocketError (/home/daniel/labs/projetos/ned-flanders-api/node_modules/@golevelup/nestjs-rabbitmq/node_modules/amqplib/lib/connection.js:355:10)
    at Connection.emit (node:events:513:28)
    at Socket.go (/home/daniel/labs/projetos/ned-flanders-api/node_modules/@golevelup/nestjs-rabbitmq/node_modules/amqplib/lib/connection.js:481:12)
    at Socket.emit (node:events:513:28)
    at emitReadable_ (node:internal/streams/readable:578:12)
    at processTicksAndRejections (node:internal/process/task_queues:82:21)

Before updating the project I was using the following versions:

Node 12.13.0
@golevelup/nestjs-rabbitmq 1.17.1
@nestjs/common: 8.0.0
@nestjs/core: 8.0.0

Now:

Node 16.20.2
@golevelup/nestjs-rabbitmq: 3.7.0
@nestjs/common: 9.0.0
@nestjs/core: 9.0.0

I also needed to install the packages:

@types/amqp: 0.2.8,
@types/amqplib: 0.10.4

Without them, when uploading the application the error was returned
image

My code:

Module

src/infrastructure/data-providers/rabbitmq/rabbitmq.module.ts

import { RabbitMQModule } from '@golevelup/nestjs-rabbitmq';
import { RabbitmqService } from './rabbitmq.service';

const rabbitMQService: Provider = {
  provide: RabbitmqService,
  useClass: RabbitmqService,
};
@Module({
  providers: [rabbitMQService],
  exports: [rabbitMQService],
  imports: [
    ConfigurationModule,
    RabbitMQModule.forRootAsync(RabbitMQModule, {
      imports: [ConfigurationModule],
      useFactory: async (configurationService: ConfigurationService) => ({
        uri: `amqp://${configurationService.rabbitUser}:${configurationService.rabbitPassword}@${configurationService.rabbitHost}:${configurationService.rabbitPort}/${configurationService.rabbitVHost}`,
        connectionInitOptions: { timeout: 10000 },
      }),
      inject: [ConfigurationService],
    }),
  ],
})
export class RabbitmqModule {}

Service
src/infrastructure/data-providers/rabbitmq/rabbitmq.service.ts

import { AmqpConnection } from '@golevelup/nestjs-rabbitmq';;

@Injectable()
export class RabbitmqService {
  constructor(
    private readonly amqpConnection: AmqpConnection,
    private readonly configuration: ConfigurationService,
  ) {}


  async publisher(routingKey: string, message: any, delay?: number): Promise<void> {
    const options = {
      appId: this.configuration.rabbitPropertiesAppId,
      timestamp: new Date().getTime(),
      contentType: 'application/json',
      correlationId: uuidv4(),
    };

    if (delay) {
      routingKey = `${routingKey}_delay`;
      options['expiration'] = delay;
    }

    try {
      await this.amqpConnection.publish(this.configuration.rabbitExchange, routingKey, message, options);
      console.log(`Mensagem enfileirada: ${JSON.stringify({ ...{ properties: options }, ...{ message } })}`);
    } catch (error) {
      console.error(
        `Erro ao enfileirar mensagem: ${JSON.stringify({ ...{ properties: options }, ...{ message } })}`,
      );
      throw new HttpException(`Erro ao enfileirar mensagem.`, HttpStatus.INTERNAL_SERVER_ERROR);
    }
  }
}

I looked in several places and in the documentation but I didn't find anything similar, does anyone have any tips on what it could be?

@dansantoss dansantoss changed the title Error when publish message: Channel closed @golevelup/nestjs-rabbitmq- error when publish message: Channel closed Feb 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant