Skip to content

Commit dda88fa

Browse files
docs(): minor tweaks to the grpc metadata docs
1 parent 282fbcd commit dda88fa

File tree

1 file changed

+35
-30
lines changed

1 file changed

+35
-30
lines changed

content/microservices/grpc.md

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,12 @@ export class HeroesController {
145145
}
146146
```
147147

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.
149149
150150
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.
151151

152152
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.
155154

156155
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.
157156

@@ -308,6 +307,21 @@ call() {
308307
}
309308
```
310309

310+
To send gRPC metadata (along with the request), you can pass a second argument, as follows:
311+
312+
```typescript
313+
call(): Observable<any> {
314+
const metadata = new Metadata();
315+
metadata.add('Set-Cookie', 'yummy_cookie=choco');
316+
317+
return this.heroesService.findOne({ id: 1 }, metadata);
318+
}
319+
```
320+
321+
> info **Hint** The `Metadata` class is imported from the `grpc` package.
322+
323+
Please note that this would require updating the `HeroesService` interface that we've defined a few steps earlier.
324+
311325
A full working example is available [here](https://github.com/nestjs/nest/tree/master/sample/04-grpc).
312326

313327
#### gRPC Streaming
@@ -372,7 +386,7 @@ The `@GrpcStreamMethod()` decorator provides the function parameter as an RxJS `
372386

373387
```typescript
374388
@GrpcStreamMethod()
375-
bidiHello(messages: Observable<any>): Observable<any> {
389+
bidiHello(messages: Observable<any>, metadata: Metadata, call: ServerDuplexStream<any, any>): Observable<any> {
376390
const subject = new Subject();
377391

378392
const onNext = message => {
@@ -388,7 +402,9 @@ bidiHello(messages: Observable<any>): Observable<any> {
388402
}
389403
```
390404

391-
> 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.
392408
393409
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.
394410

@@ -445,26 +461,26 @@ Here we used the `callback` function to send the response once processing of the
445461

446462
#### gRPC Metadata
447463

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.
449465

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).
451467

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).
453469

454470
```typescript
455471
@@filename(heroes.controller)
456472
@Controller()
457473
export class HeroesService {
458474
@GrpcMethod()
459475
findOne(data: HeroById, metadata: Metadata, call: ServerUnaryCall<any>): Hero {
460-
const srvMetadata = new Metadata();
476+
const serverMetadata = new Metadata();
461477
const items = [
462478
{ id: 1, name: 'John' },
463479
{ id: 2, name: 'Doe' },
464480
];
465481

466-
srvMetadata.add('Set-Cookie', 'yummy_cookie=choco');
467-
call.sendMetadata(srvMetadata);
482+
serverMetadata.add('Set-Cookie', 'yummy_cookie=choco');
483+
call.sendMetadata(serverMetadata);
468484

469485
return items.find(({ id }) => id === data.id);
470486
}
@@ -474,39 +490,28 @@ export class HeroesService {
474490
export class HeroesService {
475491
@GrpcMethod()
476492
findOne(data, metadata, call) {
477-
const srvMetadata = new Metadata();
493+
const serverMetadata = new Metadata();
478494
const items = [
479495
{ id: 1, name: 'John' },
480496
{ id: 2, name: 'Doe' },
481497
];
482498

483-
srvMetadata.add('Set-Cookie', 'yummy_cookie=choco');
484-
call.sendMetadata(srvMetadata);
499+
serverMetadata.add('Set-Cookie', 'yummy_cookie=choco');
500+
call.sendMetadata(serverMetadata);
485501

486502
return items.find(({ id }) => id === data.id);
487503
}
488504
}
489505
```
490-
> info **Hint** The classes `Metadata`, `ServerUnaryCall`, `ServerUnaryCall`, `ServerReadableStream` etc. are exported
491-
> from the
492-
> `grpc` package.
493-
494-
The Grpc client can also send metadata if it is sent by the second parameter:
495506

496-
```typescript
497-
getHero(): Observable<string> {
498-
const srvMetadata = new Metadata();
499-
500-
srvMetadata.add('Set-Cookie', 'yummy_cookie=choco');
507+
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).
501508

502-
return this.heroesService.findOne({ id: 1 }, srvMetadata);
503-
}
504-
```
509+
To send back metadata from the handler, use the `ServerDuplexStream#sendMetadata()` method (third handler argument).
505510

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:
507512

508513
```typescript
509-
stream.on('metadata', (metadata: Metadata) => {
510-
const getMeta = metadata.get('X-My-Meta');
514+
requestStream.on('metadata', (metadata: Metadata) => {
515+
const meta = metadata.get('X-Meta');
511516
});
512517
```

0 commit comments

Comments
 (0)