# RabbitMQ

{% embed url="<https://www.npmjs.com/package/@microservice-stack/nest-rabbitmq>" %}

{% embed url="<https://github.com/FurlanLuka/microservice-stack/tree/main/libs/nest-rabbitmq>" %}

This library provides a module that enables the usage of RabbitMQ inside your project. It comes with support for publishing events as well as subscribing to them

### Import

```typescript
import { Module } from '@nestjs/common';
import { RabbitmqModule } from '@microservice-stack/nest-rabbitmq';
import { ConfigModule, ConfigService } from '@microservice-stack/nest-config';

@Module({
  imports: [
    RabbitmqModule.register({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (configService: ConfigService) => ({
        uri: configService.get('QUEUE_URL'),
      }),
    }),
  ],
})
export class AppModule {}
```

### Publish

```typescript
export interface HelloWorldEventPayload {
  hello: string;
}

export const HELLO_WORLD_EVENT = new Event<HelloWorldEventPayload>('hello.world', 1);
```

```typescript
import { RabbitmqService } from '@microservice-stack/nest-rabbitmq';
import { HELLO_WORLD_EVENT } from './constants';

export class HelloService {
  constructor(private queueService: RabbitmqService) {}

  public sayHello(): void {
    this.queueService.publishEvent(HELLO_WORLD_EVENT, { hello: 'world' });
  }
}
```

### Subscribe

Queue controllers should be defined separately from Rest controllers as any Rest endpoints defined in them will not work.

```typescript
import { Controller } from "@nestjs/common";
import { Subscribe } from "@microservice-stack/nest-rabbitmq";
import { HELLO_WORLD_EVENT, HelloWorldEventPayload } from './constants';

@Controller()
export class HelloQueueController {
  static CONTROLLER_QUEUE_NAME = 'hello-queue';

  @Subscribe(HELLO_WORLD_EVENT, HelloQueueController.CONTROLLER_QUEUE_NAME)
  public hello(payload: HelloWorldEventPayload): void {
    console.log(payload.hello);
  }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.microservice-stack.com/introduction/nest-libraries/rabbitmq.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
