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
@@ -135,7 +135,7 @@ export class HeroesController {
135
135
@Controller()
136
136
exportclassHeroesController {
137
137
@GrpcMethod('HeroesService', 'FindOne')
138
-
findOne(data, metadata) {
138
+
findOne(data, metadata, call) {
139
139
const items = [
140
140
{ id: 1, name: 'John' },
141
141
{ id: 2, name: 'Doe' },
@@ -145,11 +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
-
The `findOne()` handler method takes two arguments, the `data` passed from the caller and `metadata` that stores gRPC request metadata.
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.
153
154
154
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.
155
156
@@ -158,7 +159,7 @@ Both `@GrpcMethod()` decorator arguments are optional. If called without the sec
> 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.
390
408
391
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.
Here we used the `callback` function to send the response once processing of the `requestStream` has been completed.
461
+
462
+
#### gRPC Metadata
463
+
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.
465
+
466
+
To read the metadata in `@GrpcMethod()` handler, use the second argument (metadata), which is of type `Metadata` (imported from the `grpc` package).
467
+
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).
508
+
509
+
To send back metadata from the handler, use the `ServerDuplexStream#sendMetadata()` method (third handler argument).
510
+
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