Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Published messages does not appear in queue #14058

Open
7 of 15 tasks
maxime-aubry opened this issue Oct 8, 2024 · 0 comments
Open
7 of 15 tasks

Published messages does not appear in queue #14058

maxime-aubry opened this issue Oct 8, 2024 · 0 comments
Labels
needs triage This issue has not been looked into

Comments

@maxime-aubry
Copy link

maxime-aubry commented Oct 8, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

I have a NestJS project with WSL and docker.
Callback endpoint is not fired each time a publish a message in queue.

Here is docker-compose.yml file

services:
  rabbitmq:
    image: rabbitmq:3-management-alpine
    container_name: rabbitmq
    hostname: rabbitmq
    volumes:
      - /var/lib/rabbitmq
    ports:
      - '5672:5672'
      - '15672:15672'
    env_file:
      - './env/.env.local'

Here my message queue module for the api what send message :

import { EnvironmentConfigModule } from '@app/shared/config/environment-config/environment-config.module';
import { EnvironmentConfigService } from '@app/shared/config/environment-config/environment-config.service';
import type { IMessageQueueConfiguration } from '@app/shared/config/environment-config/message-queue-config.interface';
import { type DynamicModule, Module, type Provider } from '@nestjs/common';
import { ClientProxyFactory, Transport } from '@nestjs/microservices';
import { MessageQueueService } from './messageQueue.service';

@Module({
  imports: [EnvironmentConfigModule],
  providers: [MessageQueueService],
  exports: [MessageQueueService],
})
export class MessageQueueModule {
  static registerRmq(service: string, queue: string): DynamicModule {
    const providers: Provider[] = [
      {
        provide: service,
        useFactory: (config: IMessageQueueConfiguration) => {
          return ClientProxyFactory.create({
            transport: Transport.RMQ,
            options: {
              urls: [config.getMessageQueueUrl()], // amqp://user:password@localhost:5672
              queue,
              queueOptions: {
                durable: true, // queue survives broker restart
              },
            },
          });
        },
        inject: [EnvironmentConfigService],
      },
    ];

    return {
      module: MessageQueueModule,
      providers,
      exports: providers,
    };
  }
}

here is the message queue service for the api what receives the message :

import { EnvironmentConfigService } from '@app/shared/config/environment-config/environment-config.service';
import type { IMessageQueueConfiguration } from '@app/shared/config/environment-config/message-queue-config.interface';
import type { IMessageQueueService } from '@app/shared/interfaces/services/messageQueue/messageQueue.service.interface';
import { Inject, Injectable } from '@nestjs/common';
import { type RmqContext, type RmqOptions, Transport } from '@nestjs/microservices';

@Injectable()
export class MessageQueueService implements IMessageQueueService {
  constructor(@Inject(EnvironmentConfigService) private config: IMessageQueueConfiguration) {}

  public getMessageQueueOptions(queue: string): RmqOptions {
    return {
      transport: Transport.RMQ,
      options: {
        urls: [this.config.getMessageQueueUrl()], // amqp://user:password@localhost:5672
        noAck: false,
        queue,
        queueOptions: {
          durable: true, // queue survives broker restart
        },
      },
    };
  }

  public acknowledgeMessage(context: RmqContext): void {
    const channel = context.getChannelRef();
    const message = context.getMessage();
    channel.ack(message);
  }
}

Here is the controller to send the message :

import { Controller, Get, Inject } from '@nestjs/common';
import type { ClientProxy } from '@nestjs/microservices';
import type { Observable } from 'rxjs';

@Controller()
export class ApiController {
  constructor(@Inject('AUTH_SERVICE') private readonly authService: ClientProxy) {}

  @Get('getUser')
  public getUser(): Observable<any> {
    return this.authService.send(
      {
        cmd: 'get-user',
      },
      {},
    );
  }
}

Here is the controller to receive the message :

import type { IMessageQueueService } from '@app/shared/interfaces/services/messageQueue/messageQueue.service.interface';
import { MessageQueueService } from '@app/shared/services/messageQueue/messageQueue.service';
import { Controller, Inject } from '@nestjs/common';
import { Ctx, MessagePattern, type RmqContext } from '@nestjs/microservices';

@Controller()
export class AuthController {
  constructor(@Inject(MessageQueueService) private readonly messageQueueService: IMessageQueueService) {}

  @MessagePattern({ cmd: 'get-user' })
  public getUser(@Ctx() context: RmqContext): string {
    this.messageQueueService.acknowledgeMessage(context);
    return 'test';
  }
}

When i run the sender, i get this log :
[Nest] 15911 - 10/08/2024, 11:40:17 AM LOG [Incoming Request on /getUser] [INFO] method=GET ip=::1 [Nest] 15911 - 10/08/2024, 11:40:17 AM ERROR [End Request for /getUser] [ERROR] method=GET status=500 code_error=null message=undefined

When i run the receiver, i get this log :
[Nest] 15681 - 10/08/2024, 11:40:17 AM WARN [Server] An unsupported message was received. It has been negative acknowledged, so it will not be re-delivered. Pattern: {"cmd":"get-user"}

In RabbitMQ web interface, i can see that my user has all rights ".*".
Your can download my project here.

What could be the problem in my project ?

thanks.

Minimum reproduction code

https:/maxime-aubry/shadow-hunters-web-api/tree/feature/2_create_auth_microservice

Steps to reproduce

bun install
docker-compose up
bun run start:dev:auth
bun run start:dev:api

Expected behavior

getUser endpoint of Auth app should return 'test'.

Package

  • I don't know. Or some 3rd-party package
  • @nestjs/common
  • @nestjs/core
  • @nestjs/microservices
  • @nestjs/platform-express
  • @nestjs/platform-fastify
  • @nestjs/platform-socket.io
  • @nestjs/platform-ws
  • @nestjs/testing
  • @nestjs/websockets
  • Other (see below)

Other package

amqplib

NestJS version

10.3.2

Packages versions

{
  "name": "nest-typescript-starter",
  "private": true,
  "version": "1.0.0",
  "description": "Nest TypeScript starter repository",
  "license": "MIT",
  "scripts": {
    "start:dev": "bun start:dev:auth && bun start:dev:api",
    "start:dev:api": "NODE_ENV=local bun --bun --watch ./apps/api/src/main.ts",
    "start:dev:auth": "NODE_ENV=local bun --bun --watch ./apps/auth/src/main.ts",
    "start:prod": "bun ./apps/api/src/main.ts",
    "test": "NODE_ENV=test bun --bun test",
    "test:debug": "NODE_ENV=test bun --bun test --inspect --break",
    "lint": "biome check .",
    "lint:fix": "biome check --write ."
  },
  "dependencies": {
    "@nestjs/common": "^10.3.2",
    "@nestjs/config": "^3.2.3",
    "@nestjs/core": "^10.3.2",
    "@nestjs/microservices": "^10.4.4",
    "@nestjs/platform-express": "^10.3.2",
    "@nestjs/swagger": "^7.4.2",
    "@types/cookie-parser": "^1.4.7",
    "amqp-connection-manager": "^4.1.14",
    "amqplib": "^0.10.4",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.14.1",
    "cookie-parser": "^1.4.6",
    "reflect-metadata": "^0.2.1",
    "rxjs": "^7.8.1"
  },
  "devDependencies": {
    "@biomejs/biome": "^1.9.3",
    "@nestjs/cli": "^10.3.1",
    "@nestjs/schematics": "^10.1.0",
    "@nestjs/testing": "^10.3.2",
    "@swc/cli": "^0.3.9",
    "@swc/core": "^1.4.0",
    "@types/express": "^4.17.21",
    "@types/node": "^20.11.16",
    "@types/supertest": "^6.0.2",
    "prettier": "^3.2.5",
    "source-map-support": "^0.5.21",
    "supertest": "^6.3.4",
    "ts-loader": "^9.5.1",
    "ts-node": "^10.9.2",
    "tsconfig-paths": "^4.2.0",
    "typescript": "^5.3.3",
    "@types/bun": "latest"
  },
  "module": "index.ts",
  "type": "module"
}

Node.js version

No response

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

I use Docker with WSL (Ubuntu) and Bun.

@maxime-aubry maxime-aubry added the needs triage This issue has not been looked into label Oct 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage This issue has not been looked into
Projects
None yet
Development

No branches or pull requests

1 participant