Transport EventBus for NestJs
npm install nestjs-transport-eventbus





TransportEventBusModule into a willing module, example below:typescript
import { TransportEventBusModule } from 'nestjs-transport-eventbus';@Module({
imports: [
TransportEventBusModule
],
controllers: [AppController],
providers: [
AppService
],
})
export class AppModule {
}
`
TransportEventBusModule applies two arguments:\
publishers - array of transport publishers which are based on ClientProxy\
providers - the additional providers for module$3
For creating a transport publisher there is enough to implement the following steps:
1. Implement RabbitPublisher, example below:\
`typescript
import { Injectable } from '@nestjs/common';
import { ClientProxy, Transport, Client} from '@nestjs/microservices';
import { Publisher } from 'nestjs-transport-eventbus';@Injectable()
@Publisher(Transport.RMQ)//Choose the appropriate type of transport in this case
RMQ
export class RabbitPublisher {
@Client({
transport: Transport.RMQ,
options: {
urls: ['amqp://rabbit:rabbit@rabbitmq:5672'],
queue: 'event_service_queue',
queueOptions: {
durable: true,
},
},
})
client: ClientProxy;
}
`
2. Inject RabbitPublisher into TransportEventBusModule, example below:
`typescript
import { Module } from '@nestjs/common';
import { TransportEventBusModule } from 'nestjs-transport-eventbus';
import { RabbitPublisher } from '...';@Module({
imports: [
TransportEventBusModule.forRoot({
publishers: [RabbitPublisher]
})
],
controllers: [],
providers: [
],
})
export class AppModule {
}
`
3. Create an event for publisher, example below:
`typescript
import { TransportType, ExcludeDef } from 'nestjs-transport-eventbus';
import { Transport } from '@nestjs/microservices';@TransportType(Transport.RMQ)
export class RabbitEvent {
constructor(
readonly message: string
) {
}
}
`
> Notice: By default, events are handling on both sides broadcasting server and receiving server, for changing this situation add @ExcludeDef() declaration,
>can look like below:
>\
>....\
>@TransportType(Transport.RMQ)\
>@ExcludeDef()\
>export class RabbitEvent\
>...
>
4.Inject TRANSPORT_EVENT_BUS_SERVICE, example below:
`typescript
import { Inject, Injectable } from '@nestjs/common';
import { TRANSPORT_EVENT_BUS_SERVICE } from 'nestjs-transport-eventbus';
import { IEventBus } from '@nestjs/cqrs';
import { DefaultEvent } from '...';
import { RabbitEvent } from '...'
@Injectable()
export class AppService {
constructor(
@Inject(TRANSPORT_EVENT_BUS_SERVICE) private readonly eventBus: IEventBus
){ }
rabbitEvent(): void {
this.eventBus.publish(new RabbitEvent('Pass some param'));
}
}
`
> Notice: Events via AggregateRoot
`typescript
import { CommandHandler, EventPublisher, ICommandHandler } from '@nestjs/cqrs';
import { TryAggregateRootCommand } from '...';
import { Inject } from '@nestjs/common';
import { TRANSPORT_EVENT_BUS_PUBLISHER } from 'nestjs-transport-eventbus';
import { TestModel } from '...';@CommandHandler(TryAggregateRootCommand)
export class TryAggregateRootCommandHandler implements ICommandHandler {
constructor(
@Inject(TRANSPORT_EVENT_BUS_PUBLISHER) private readonly publisher: EventPublisher
) {
}
async execute(command: TryAggregateRootCommand) {
const {message} = command;
const aggregator = this.publisher.mergeObjectContext(
new TestModel()
);
aggregator.applyEvent(message);
aggregator.commit();
}
}
`
5. For handling the broadcasted event on receiving side can look like the following:
`typescript
import { Controller, Inject } from '@nestjs/common';
import { EventPattern } from '@nestjs/microservices';
import { TRANSPORT_EVENT_BUS_PATTERN, TRANSPORT_EVENT_BUS_SERVICE, TransportEvent } from 'nestjs-transport-eventbus';
import { IEvent, IEventBus } from '@nestjs/cqrs';@Controller()
export class AppService {
constructor(
@Inject(TRANSPORT_EVENT_BUS_SERVICE) private readonly eventBus: IEventBus
){
}
@EventPattern(TRANSPORT_EVENT_BUS_PATTERN)
handle(@TransportEvent() event: IEvent): void {
this.eventBus.publish(event);
}
`
> Notice: TRANSPORT_EVENT_BUS_PATTERN`- can pass via .env