You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/microservices/grpc.md
+35-30Lines changed: 35 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -145,13 +145,12 @@ export class HeroesController {
145
145
}
146
146
```
147
147
148
-
> info **Hint** The `@GrpcMethod()` decorator is imported from the `@nestjs/microservices` package.
148
+
> info **Hint** The `@GrpcMethod()` decorator is imported from the `@nestjs/microservices` package, while `Metadata` and `ServerUnaryCall` from the `grpc` package.
149
149
150
150
The decorator shown above takes two arguments. The first is the service name (e.g., `'HeroesService'`), corresponding to the `HeroesService` service definition in `hero.proto`. The second (the string `'FindOne'`) corresponds to the `FindOne()` rpc method defined within `HeroesService` in the `hero.proto` file.
151
151
152
152
The `findOne()` handler method takes three arguments, the `data` passed from the caller, `metadata` that stores gRPC
153
-
request metadata and `call` to obtain the `GrpcCall` object properties such as `sendMetadata` for send metadata to client.
154
-
153
+
request metadata and `call` to obtain the `GrpcCall` object properties such as `sendMetadata` for send metadata to client.
155
154
156
155
Both `@GrpcMethod()` decorator arguments are optional. If called without the second argument (e.g., `'FindOne'`), Nest will automatically associate the `.proto` file rpc method with the handler based on converting the handler name to upper camel case (e.g., the `findOne` handler is associated with the `FindOne` rpc call definition). This is shown below.
157
156
@@ -308,6 +307,21 @@ call() {
308
307
}
309
308
```
310
309
310
+
To send gRPC metadata (along with the request), you can pass a second argument, as follows:
> info **Hint** For supporting full-duplex interaction with the `@GrpcStreamMethod()` decorator, the controller method must return an RxJS `Observable`.
405
+
> warning **Warning** For supporting full-duplex interaction with the `@GrpcStreamMethod()` decorator, the controller method must return an RxJS `Observable`.
406
+
407
+
> info **Hint** The `Metadata` and `ServerUnaryCall` classes/interfaces are imported from the `grpc` package.
392
408
393
409
According to the service definition (in the `.proto` file), the `BidiHello` method should stream requests to the service. To send multiple asynchronous messages to the stream from a client, we leverage an RxJS `ReplySubject` class.
394
410
@@ -445,26 +461,26 @@ Here we used the `callback` function to send the response once processing of the
445
461
446
462
#### gRPC Metadata
447
463
448
-
gRPC metadata is additional data that may be useful during the processing of requests and responses, but is not part of the actual application data. Metadata may include authentication tokens, request identifiers and tags for monitoring purposes, and data information such as the number of records in a data set.
464
+
Metadata is information about a particular RPC call in the form of a list of key-value pairs, where the keys are strings and the values are typically strings but can be binary data. Metadata is opaque to gRPC itself - it lets the client provide information associated with the call to the server and vice versa. Metadata may include authentication tokens, request identifiers and tags for monitoring purposes, and data information such as the number of records in a data set.
449
465
450
-
To read the metadata in `@GrpcMethod()` use the second handler metadata argument, which returns the gRPC class `Metadata`.
466
+
To read the metadata in `@GrpcMethod()`handler, use the second argument (metadata), which is of type `Metadata` (imported from the `grpc` package).
451
467
452
-
You can send metadata from `@GrpcMethod()` using the function `sendMetadata()`which is provided by the object `GrpcCall` in the third handler argument.
468
+
To send back metadata from the handler, use the `ServerUnaryCall#sendMetadata()`method (third handler argument).
Likewise, to read the metadata in handlers annotated with the `@GrpcStreamMethod()` handler ([subject strategy](microservices/grpc#subject-strategy)), use the second argument (metadata), which is of type `Metadata` (imported from the `grpc` package).
To send back metadata from the handler, use the `ServerDuplexStream#sendMetadata()` method (third handler argument).
505
510
506
-
Stream can receive metadata using a `metadata` event:
511
+
To read metadata from within the [call stream handlers](microservices/grpc#call-stream-handler) (handlers annotated with `@GrpcStreamCall()` decorator), listen to the `metadata` event on the `requestStream` reference, as follows:
0 commit comments