Skip to content

Commit 526585a

Browse files
Merge pull request #1246 from barbarosh/feature/message-pattern-bind-transport
docs(microservices): add instruction to use transport in MessagePattern
2 parents 2815b45 + 635e90f commit 526585a

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

content/faq/hybrid-application.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,35 @@ await app.startAllMicroservicesAsync();
3535
await app.listen(3001);
3636
```
3737

38+
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.
39+
40+
```typescript
41+
@@filename()
42+
@MessagePattern('time.us.*', Transport.NAST)
43+
getDate(@Payload() data: number[], @Ctx() context: NatsContext) {
44+
console.log(`Subject: ${context.getSubject()}`); // e.g. "time.us.east"
45+
return new Date().toLocaleTimeString(...);
46+
}
47+
@MessagePattern({ cmd: 'time.us' }, Transport.TCP)
48+
getTCPDate(@Payload() data: number[]) {
49+
return new Date().toLocaleTimeString(...);
50+
}
51+
@@switch
52+
@Bind(Payload(), Ctx())
53+
@MessagePattern('time.us.*', Transport.NAST)
54+
getDate(data, context) {
55+
console.log(`Subject: ${context.getSubject()}`); // e.g. "time.us.east"
56+
return new Date().toLocaleTimeString(...);
57+
}
58+
@Bind(Payload(), Ctx())
59+
@MessagePattern({ cmd: 'time.us' }, Transport.TCP)
60+
getTCPDate(data, context) {
61+
return new Date().toLocaleTimeString(...);
62+
}
63+
```
64+
65+
> info **Hint** `@Payload()`, `@Ctx()`, `Transport` and `NatsContext` are imported from `@nestjs/microservices`.
66+
3867
#### Sharing configuration
3968

4069
By default a hybrid application will not inherit global pipes, interceptors, guards and filters configured for the main (HTTP-based) application.

0 commit comments

Comments
 (0)