When @EventPattern
is supplied with a wildcard, e.g. sensor/+/status
, a There is no matching event handler defined in the remote service
error is logged (similar: #2236). However, when supplying the full topic string, e.g. sensor/1/status
, the event is caught as normal.
@EventPattern('sensor/+/status')
async handleStatus(data: Record<string, string>) {
console.log(data);
}
Receiving a message with topic sensor/1/status
should be handled by both handlers with pattern sensor/+/status
and handlers with pattern sensor/#
.
When receiving an event, check for wildcards in the registered patterns, such as +
and #
, before matching to handlers. It might have similar solution to #2447
Nest version: 6.6.7
For Tooling issues:
- Node version: 10.16.0
- Platform: Windows
Others:
For anyone who are still looking for a solution, please use this custom strategy:
import { MessageHandler } from '@nestjs/microservices/interfaces';
import { ServerMqtt } from '@nestjs/microservices/server';
import {
MQTT_WILDCARD_ALL,
MQTT_WILDCARD_SINGLE,
} from './constants';
import { matches } from './mqtt-pattern';
export class CustomServerMqtt extends ServerMqtt {
public getHandlerByPattern(pattern: string): MessageHandler | null {
if (this.messageHandlers.has(pattern)) {
return this.messageHandlers.get(pattern);
}
for (const [key, value] of this.messageHandlers) {
if ((key.indexOf(MQTT_WILDCARD_SINGLE) !== -1
|| key.indexOf(MQTT_WILDCARD_ALL) !== -1)
&& matches(key, pattern)) {
return value;
}
}
return null;
}
}
and bootstrap it as ussual
export async function bootstrapMicroservice() {
const microservice = await NestFactory.createMicroservice(ApplicationModule, {
strategy: new CustomServerMqtt({
url: `mqtt://localhost:1881`,
}),
});
await microservice.listenAsync();
}
I believe this can be closed no?
Correct @wesselvdv. This feature is already available in the framework.
Most helpful comment
For anyone who are still looking for a solution, please use this custom strategy:
and bootstrap it as ussual