Skip to content

fix(microservices): ignore an mqtt server's own replies under wildcards - #18418

Merged
kamilmysliwiec merged 1 commit into
nestjs:masterfrom
pavstev:fix/mqtt-skip-own-replies
Nov 6, 2026
Merged

kamilmysliwiec merged 1 commit into
nestjs:masterfrom
pavstev:fix/mqtt-skip-own-replies

Conversation

@pavstev

@pavstev pavstev commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

ServerMqtt publishes its replies on <topic>/reply with the same client that subscribes to the handlers' filters. A filter that ends in # (for example @MessagePattern('orders/#')) also matches orders/1/reply, so the broker hands the server its own reply. handleMessage takes it for a foreign message and runs the handler a second time, with the reply object as its input. Side effects of the handler run twice for one request. An exact topic and a + filter do not match the reply topic, so they are not affected.

This is the MQTT twin of #18125, which fixed the same thing for the Redis server.

Issue Number: N/A

What is the new behavior?

handleMessage skips a message when all of these hold, as isOwnReply does in ServerRedis:

  • the topic ends in /reply,
  • the packet has an id and no pattern (a reply, not a request or an event),
  • no handler is registered on a filter that ends in /reply and matches the topic.

MQTT wildcards work on topic levels, so the last check uses the existing matchMqttPattern and removeHandlerKeySharedPrefix (shared subscriptions). A handler on orders/+/reply or an exact orders/1/reply still gets the replies. Requests published to a reply topic still arrive, because they carry a pattern. No option is added and no default changes.

Tests in server-mqtt.spec.ts (4 of the new ones fail on master, the others pin the guard):

  • a reply on orders/1/reply is not dispatched to a message handler, an event handler or a shared subscription on orders/#;
  • it is not dispatched to users/# when another handler targets reply topics;
  • it still reaches handlers on orders/+/reply, orders/1/reply, +/+/reply and a shared orders/+/reply;
  • a request, a request published to a reply topic and an event without an id are still dispatched;
  • orders/+ and the exact topic are not reached by the reply.

I removed each condition of the guard one at a time and each time a test went red.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

A message that is not from Nest, has an id and no pattern, and is published to a topic ending in /reply is now skipped unless a handler targets reply topics. Before, a wildcard event handler received it. This is the same rule as in the Redis server.

One limit, as before this change: the server looks up one handler per topic. If a # handler is registered before a handler on a reply filter, the lookup still returns the # one for the reply. This PR does not change that.

@kamilmysliwiec
kamilmysliwiec merged commit 672a762 into nestjs:master Nov 6, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants