RabbitMQ is a messaging broker - an intermediary for messaging. It gives your applications a common platform to send and receive messages, and your messages a safe place to live until received.
You can to use gadin-rabbit package easily and setup it.
npm install gadin-rabbit
wait for the installation to finish.
You've added below code in your root module:
@Module({
imports: [
RmqModule.register({
uri: 'amqp://guest:guest@localhost:5672',
name : "services",
wait : true ,
timeout : 20000,
type : 'topic',
prefetchCount : 10
}),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {
}
1. The name, wait, timeout, type and prefetchCount are optional.
2. Default value of type is topic.
You've to add these decorators above the functions:
If the return value of your function is void, then you have to use this method:
@Subscribe({
routingKey: 'user.add',
queue: 'user-add',
})
addUser(objUser:any) : void
{
// add user...
}
If the return value of your function is not void, then you have to use this method:
@Rpc({
routingKey: 'user.get',
queue: 'user-get',
})
getUser(id:number) : User
{
// get user...
}
You've to inject RmqService in your service and use rabbit methods like below:
@Injectable()
export class AppService {
constructor(private broker: RmqService) {
}
}
If you want consume your messages should be use these decorators:
If you want to call void message, then you've to use this way:
await this.broker.publish('user.add', payload);
If you want to call don't void message, then you've to use this way:
return await this.broker.request<any>({ routingKey, payload, timeout });
Import RmqModule, RmqService, Rpc and Subscribe from the gadin-rabbit