To bind @MessagePattern() to only one transport strategy (for example, MQTT) in a hybrid application with multiple microservices, we can pass the second argument of type Transport which is an enum with all the built-in transport strategies defined.
@MessagePattern('time.us.*',Transport.NATS)getDate(@Payload()data:number[],@Ctx()context:NatsContext){console.log(`Subject: ${context.getSubject()}`);// e.g. "time.us.east"returnnewDate().toLocaleTimeString(...);}@MessagePattern({cmd:'time.us'},Transport.TCP)getTCPDate(@Payload()data:number[]){returnnewDate().toLocaleTimeString(...);}
@Bind(Payload(),Ctx())@MessagePattern('time.us.*',Transport.NATS)getDate(data,context){console.log(`Subject: ${context.getSubject()}`);// e.g. "time.us.east"returnnewDate().toLocaleTimeString(...);}@Bind(Payload(),Ctx())@MessagePattern({cmd:'time.us'},Transport.TCP)getTCPDate(data,context){returnnewDate().toLocaleTimeString(...);}
Hint
@Payload(), @Ctx(), Transport and NatsContext are imported from @nestjs/microservices.
By default a hybrid application will not inherit global pipes, interceptors, guards and filters configured for the main (HTTP-based) application.
To inherit these configuration properties from the main application, set the inheritAppConfig property in the second argument (an optional options object) of the connectMicroservice() call, as follow: