Hi,
We use @nestjs-plus/rabbitmq
to communicate with rabbitmq.
If we use it directly in a given service (with the implementation associated with this level), it is functional.
But we want to have a RabbitModule
module that would contain the services to publish or receive the messages and use this service in other modules. But we face either errors of transpiration or errors where the service is not recognized.
So, we wonder if this feature is feasible with @nestjs-plus/rabbitmq
and if so, what are we doing wrong?
rabbitmq.service.ts
import { Injectable } from '@nestjs/common';
import { AmqpConnection } from '@nestjs-plus/rabbitmq';
@Injectable()
export class RabbitmqClientService {
constructor(private readonly amqpConnection: AmqpConnection) {}
public async publishMessage(exchange: string, subscribeRoute: string, message: {}) {
await this.amqpConnection.publish(exchange, subscribeRoute, {
message,
});
return {
result: 'Published message',
};
}
}
rabbit.module.ts :
import { Module } from '@nestjs/common';
import { RabbitMQModule } from '@nestjs-plus/rabbitmq';
import { AmqpConnection } from '@nestjs-plus/rabbitmq';
import { RabbitmqClientService } from './services/rabbitmq.service';
import { RabbitmqController } from './controllers/rabbitmq.controller';
@Module({
imports: [
RabbitMQModule.forRootAsync({
useFactory: () => ({
exchanges: [
{
name: 'exchange1',
type: 'topic',
},
{
name: 'exchange2',
type: 'fanout',
},
],
uri: 'amqp://rabbitmq:rabbitmq@localhost:5672',
}),
}),
AmqpConnection
],
controllers: [RabbitmqController],
providers: [AmqpConnection, RabbitmqClientService],
exports: [RabbitmqClientService]
})
export class RabbitModule {}
PodcastController.ts :
(...)
public constructor(
private readonly podcastService: PodcastService,
private readonly rabbitmqClientService: RabbitmqClientService,
) {}
(...)
})
public async getListPodcast(): Promise<Redaction[]> {
const queue: QueueDto = {
name: 'queue_test',
};
const exchange = 'exchange_test';
const podcasts = await this.podcastService.findAll();
const message: RabbitmqEvent = {
eventName: EventName.PodcastListWasRequested,
objectId: uuid(),
objectVersion: 1,
commandPayload: {
podcasts,
},
eventCreatedBy: 'fdf9859f-6f51-42b0-b7a5-b2c9f697cad6',
eventCreatedAt: Date.now(),
};
await this.rabbitmqClientService.publishMessage(
queue.name,
exchange,
message
);
return podcasts;
}
we have this error
Nest can't resolve dependencies of the RabbitmqClientService (?). Please make sure that the argument AmqpConnection at index [0] is available in the PodcastModule context.
Potential solutions:
- If AmqpConnection is a provider, is it part of the current PodcastModule?
- If AmqpConnection is exported from a separate @Module, is that module imported within PodcastModule?
@Module({
imports: [ /* the Module containing AmqpConnection */ ]
})
+1ms
if we modify the module rabbit.module.ts
as well :
controllers: [RabbitmqController],
providers: [AmqpConnection, RabbitmqClientService],
exports: [RabbitmqClientService, AmqpConnection],
and podcast.module.ts
:
controllers: [PodcastController],
providers: [PodcastService, RabbitmqClientService, AmqpConnection, ...queryHandler],
exports: [RabbitmqClientService],
We do not have a compilation problem, however, when we call the getListPodcast
method (in REST with a GET), we have this error:
TypeError: Cannot read property 'publish' of undefined