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

Topic exchanges support for RabbitMQ (routing) #14535

Closed
3 of 15 tasks
its-dibo opened this issue Jan 29, 2025 · 6 comments
Closed
3 of 15 tasks

Topic exchanges support for RabbitMQ (routing) #14535

its-dibo opened this issue Jan 29, 2025 · 6 comments

Comments

@its-dibo
Copy link

its-dibo commented Jan 29, 2025

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

I created a microservice using NestFactory.createMicroservice and added a @EventPattern like this

@EventPattern('msg')
msg(){
 console.log('msg')
}

@EventPattern('*')
all(){
 console.log('all')
}

navigating to RabbitMQ UI, I can see the queue created successfully and bind to the default channel, but when I try to publish a message from there

{ "pattern": "msg" }

RabbitMQ tells that the message is routed, but I always get ERROR [Server] There is no matching event handler defined in the remote service. Event pattern: msg

I tried to create a minimal repo, but the app doesn't continue after app.listen() statement, I,.e app2 never logged
anyway, I just created a basic microservice in a totally new app
https://stackblitz.com/edit/nestjs-typescript-starter-qknpqrbp?file=src%2Fmain.ts

Minimum reproduction code

https://stackblitz.com/edit/nestjs-typescript-starter-qknpqrbp?file=src%2Fmain.ts

Steps to reproduce

1- replace NestFactory.create() with NestFactory.createMicroservice() and provide urls and queue
2- navigate to the RabbitMQ management UI to confirm the queue is created
3- publish a message from the created queue in RabbitMQ UI, the message is routed
4- NestJs logs an error that the pattern has not defined

Expected behavior

the message is catched by @EventPattern('msg') or even @EventPattern('*')

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

No response

NestJS version

11.0.6

Packages versions

"@nestjs/core": "^11.0.6"
"dependencies": {
    "@nestjs/common": "^11.0.6",
    "@nestjs/config": "^4.0.0",
    "@nestjs/core": "^11.0.6",
    "@nestjs/event-emitter": "^3.0.0",
    "@nestjs/microservices": "^11.0.6",
    "@nestjs/platform-express": "^11.0.6",
    "@nestjs/platform-fastify": "^11.0.6",
    "@nestjs/platform-socket.io": "^11.0.6",
    "@nestjs/swagger": "^11.0.3",
    "@nestjs/typeorm": "^11.0.0",
    "@nestjs/websockets": "^11.0.6",
    "amqp-connection-manager": "^4.1.14",
    "amqplib": "^0.10.5",
    "array-flatten": "^3.0.0",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.14.1",
    "fastify": "^5.2.1",
    "kill-port": "^2.0.1",
    "pg": "^8.13.1",
    "reflect-metadata": "^0.2.2",
    "socket.io": "^4.8.1",
    "tslib": "^2.8.1",
    "typeorm": "^0.3.20"
  },
  "devDependencies": {
    "@jest/globals": "^29.7.0",
    "@nestjs/cli": "^11.0.2",
    "@nestjs/schematics": "^11.0.0",
    "@nestjs/testing": "^11.0.6",
    "@types/node": "^22.12.0",
    "dotenv": "^16.4.7",
    "kill-port": "^2.0.1",
    "nodemon": "^3.1.9",
    "prettier": "^3.4.2",
    "semantic-release": "^24.2.1",
    "semantic-release-monorepo": "^8.0.2",
    "ts-node": "^10.9.2",
    "tsconfig-paths": "^4.2.0",
    "typescript": "^5.7.3"
  }
</details>

Node.js version

v22.12.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

@its-dibo its-dibo added the needs triage This issue has not been looked into label Jan 29, 2025
@kamilmysliwiec
Copy link
Member

The built-in RabbitMQ transporter uses sendToQueue (sends messages/events directly to the queue), not publish which means we currently don't support Topic Exchanges = no wildcards (*, #).

This sounds like a useful feature though, I'll look into that soon

@kamilmysliwiec kamilmysliwiec added type: enhancement 🐺 scope: microservices and removed needs triage This issue has not been looked into labels Jan 30, 2025
@kamilmysliwiec kamilmysliwiec changed the title Nest always reports: There is no matching event handler defined in the remote service Support topic exchanges for RabbitMQ (routing) Jan 30, 2025
@kamilmysliwiec kamilmysliwiec changed the title Support topic exchanges for RabbitMQ (routing) Topic exchanges support for RabbitMQ (routing) Jan 30, 2025
@its-dibo
Copy link
Author

its-dibo commented Jan 30, 2025

I also tested using publish via postman, and the same error occurs

curl --location 'http://localhost:15672/api/exchanges/%2F//publish' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic Z3Vlc3Q6Z3Vlc3Q=' \
--data '{
    "properties": {},
    "routing_key": "trips",
    "payload": "{\"pattern\": \"msg\", \"data\": {} }",
    "payload_encoding": "string"
  }'

also changing publish to sendToQueue doesn't help (no log at all)
@kamilmysliwiec

@kamilmysliwiec
Copy link
Member

Because your queue isn't bound to that routing key - I'll make sure to include that in the PR

@its-dibo
Copy link
Author

the queue and the routing key are the same, both are trips, or in the minimal repro, I just use the Nest's example cats_queue

@kamilmysliwiec
Copy link
Member

Let's track this here #14540

@its-dibo
Copy link
Author

wow! this is interesting! @kamilmysliwiec

this PR solves one issue: the topic exchange (I'm waiting for the full merge to give it a go)

still, there is another issue, routing a message to an existing pattern using the HTTP API or RabbitMQ UI causes RROR [Server] There is no matching event handler defined in the remote service. Event pattern: msg even if the pattern already exists

steps

// queue: "trips", pattern: "msg"

@EventPattern('msg')
msg(){
 console.log('msg')
}

request

curl --location 'http://localhost:15672/api/exchanges/%2F//publish' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic Z3Vlc3Q6Z3Vlc3Q=' \
--data '{
    "properties": {},
    "routing_key": "trips",
    "payload": "{\"pattern\": \"msg\", \"data\": {} }",
    "payload_encoding": "string"
  }'

response:

{ "pattern": "msg" }

Nest Logs:

RROR [Server] There is no matching event handler defined in the remote service. Event pattern: msg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants