Skip to content

Commit eabef6c

Browse files
author
alex
committed
docs(microservices): add instruction to use transport in MessagePattern
1 parent d7d0640 commit eabef6c

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

content/microservices/basics.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,34 @@ getDate(data, context) {
205205
}
206206
```
207207

208-
> info **Hint** `@Payload()`, `@Ctx()` and `NatsContext` are imported from `@nestjs/microservices`.
208+
For bind `@MessagePattern()` only to one transport of microservices in hybrid application with multiple microservices can use second argument which should get value from `Transport` enum
209+
210+
```typescript
211+
@@filename()
212+
@MessagePattern('time.us.*', Transport.NAST)
213+
getDate(@Payload() data: number[], @Ctx() context: NatsContext) {
214+
console.log(`Subject: ${context.getSubject()}`); // e.g. "time.us.east"
215+
return new Date().toLocaleTimeString(...);
216+
}
217+
@MessagePattern({ cmd: 'time.us' }, Transport.TCP)
218+
getTCPDate(@Payload() data: number[]) {
219+
return new Date().toLocaleTimeString(...);
220+
}
221+
@@switch
222+
@Bind(Payload(), Ctx())
223+
@MessagePattern('time.us.*', Transport.NAST)
224+
getDate(data, context) {
225+
console.log(`Subject: ${context.getSubject()}`); // e.g. "time.us.east"
226+
return new Date().toLocaleTimeString(...);
227+
}
228+
@Bind(Payload(), Ctx())
229+
@MessagePattern({ cmd: 'time.us' }, Transport.TCP)
230+
getTCPDate(data, context) {
231+
return new Date().toLocaleTimeString(...);
232+
}
233+
```
234+
235+
> info **Hint** `@Payload()`, `@Ctx()`, `Transport` and `NatsContext` are imported from `@nestjs/microservices`.
209236
210237
#### Client
211238

0 commit comments

Comments
 (0)