gRPC transport helps your NestJS app talk to other apps fast and clearly using a special language called Protocol Buffers.
gRPC transport in NestJS
import { Controller } from '@nestjs/common'; import { GrpcMethod } from '@nestjs/microservices'; @Controller() export class MyGrpcService { @GrpcMethod('ServiceName', 'MethodName') methodName(data: RequestType): ResponseType { // handle request and return response } }
@GrpcMethod decorator links your method to a gRPC service and method name.
Request and response types come from your Protocol Buffers definitions.
import { Controller } from '@nestjs/common'; import { GrpcMethod } from '@nestjs/microservices'; @Controller() export class HeroService { @GrpcMethod('HeroService', 'FindOne') findOne(data: { id: number }): { name: string } { return { name: 'Superman' }; } }
import { Controller } from '@nestjs/common'; import { GrpcMethod } from '@nestjs/microservices'; @Controller() export class MathService { @GrpcMethod('MathService', 'Sum') sum(data: { a: number; b: number }): { result: number } { return { result: data.a + data.b }; } }
This NestJS gRPC service has one method called multiply. It takes two numbers and returns their product.
import { Controller } from '@nestjs/common'; import { GrpcMethod } from '@nestjs/microservices'; @Controller() export class CalculatorService { @GrpcMethod('CalculatorService', 'Multiply') multiply(data: { x: number; y: number }): { product: number } { return { product: data.x * data.y }; } }
Make sure your .proto files match the service and method names exactly.
Use NestJS microservices options to configure gRPC transport in your main app module.
Test your gRPC methods with tools like BloomRPC or Evans for easy debugging.
gRPC transport lets NestJS apps talk fast and clearly using Protocol Buffers.
Use @GrpcMethod to connect your methods to gRPC service methods.
It is great for microservices and streaming data.
Practice
gRPC transport in a NestJS application?Solution
Step 1: Understand gRPC transport role
gRPC transport in NestJS is designed to enable fast communication between services using Protocol Buffers.Step 2: Compare with other options
Options B, C, and D describe other unrelated features like REST APIs, database, or auth, which are not the main purpose of gRPC transport.Final Answer:
To enable fast and clear communication between microservices using Protocol Buffers -> Option AQuick Check:
gRPC transport = fast microservice communication [OK]
- Confusing gRPC with REST APIs
- Thinking gRPC manages databases
- Assuming gRPC handles authentication
Solution
Step 1: Recall NestJS gRPC decorators
The correct decorator to connect a method to a gRPC service method is@GrpcMethod.Step 2: Eliminate incorrect options
@GrpcService, @GrpcHandler, and @GrpcClient are not valid decorators for this purpose in NestJS.Final Answer:
@GrpcMethod -> Option AQuick Check:
Method to gRPC service = @GrpcMethod [OK]
- Using @GrpcService instead of @GrpcMethod
- Confusing client decorators with method decorators
- Misspelling the decorator name
{ id: 5 }?
@GrpcMethod('HeroService', 'FindOne')
findOne(data: { id: number }): { name: string } {
const heroes = [{ id: 1, name: 'Superman' }, { id: 5, name: 'Batman' }];
return { name: heroes.find(hero => hero.id === data.id)?.name || 'Unknown' };
}Solution
Step 1: Analyze the input and method logic
The method receives{ id: 5 }and searches the heroes array for a hero with id 5.Step 2: Find matching hero
Hero with id 5 is { id: 5, name: 'Batman' }, so it returns { name: 'Batman' }.Final Answer:
{ name: 'Batman' } -> Option DQuick Check:
Input id 5 returns Batman [OK]
- Returning Superman instead of Batman
- Returning Unknown when hero exists
- Returning undefined instead of fallback
@GrpcMethod('HeroService', 'FindOne')
findOne(data: { id: number }): { name: string } {
const heroes = [{ id: 1, name: 'Superman' }];
return heroes.find(hero => hero.id === data.id).name;
}Solution
Step 1: Understand the find method behavior
If no hero matchesdata.id,findreturns undefined.Step 2: Identify the error
Accessing.nameon undefined causes a runtime error.Final Answer:
Possible runtime error if hero not found -> Option CQuick Check:
Accessing property on undefined causes error [OK]
- Ignoring undefined result from find()
- Assuming decorator name is wrong
- Thinking return type annotation is missing
Solution
Step 1: Understand streaming method return types
Streaming gRPC methods in NestJS return an Observable to send multiple messages over time.Step 2: Evaluate options
@GrpcMethod('ChatService', 'StreamMessages') streamMessages(data: { userId: number }): Observable<{ message: string }> { // implementation } returns Observable, suitable for streaming. Options A and C return single responses (Promise or object). @GrpcMethod('ChatService', 'StreamMessages') streamMessages(data: { userId: number }): void { // implementation } returns void, which is invalid.Final Answer:
Method returning Observable for streaming -> Option BQuick Check:
Streaming gRPC method returns Observable [OK]
- Using Promise instead of Observable for streaming
- Returning plain object for streaming method
- Using void return type for gRPC method
Ready to prove it?
Pass the test and this pattern moves unknown
