nestjs
nestjs copied to clipboard
@golevelup/nestjs-rabbitmq- error when publish message: Channel closed
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
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?
Same here!!
Me tooπ
Node 16 is not supported anymore, I recommend upgrading it. The same to nestjs, you're using v9, we've set the defaults to v10.0.5
and this may interfere with the dependency injection.
Anyway, I can try to help you but I will solicit a reproduction ready to use in GH if you could build one so that I can pull it down locally and try to replicate what you're seeing