📖 Scenario: You are creating a small service that uses gRPC to communicate between a client and a server. This service will greet users by their name.
🎯 Goal: Build a NestJS gRPC service that receives a name and returns a greeting message.
📋 What You'll Learn
Create a protobuf file defining the greeting service and messages
Configure NestJS to use gRPC transport with the protobuf
Implement the service method to return a greeting
Set up the main application to start the gRPC server
💡 Why This Matters
🌍 Real World
gRPC is used in microservices to enable fast, efficient communication between services, especially in distributed systems.
💼 Career
Understanding how to build and configure gRPC services with NestJS is valuable for backend developers working with modern microservice architectures.
Progress0 / 4 steps
1
Create the protobuf file for the greeting service
Create a file named hero.proto with a package named hero. Define a service called HeroService with an RPC method SayHello that takes a HelloRequest message and returns a HelloReply message. The HelloRequest message should have a string field name. The HelloReply message should have a string field message.
NestJS
Hint
Remember to use syntax = "proto3"; at the top and define the package and service exactly as described.
2
Configure NestJS to use gRPC transport
In your main.ts file, import NestFactory and Transport from @nestjs/microservices. Create a microservice using NestFactory.createMicroservice with transport: Transport.GRPC and options specifying the package as hero and the protoPath as the path to hero.proto. Then start the microservice.
NestJS
Hint
Use join(__dirname, './hero.proto') to set the protoPath and specify transport: Transport.GRPC.
3
Implement the HeroService with SayHello method
Create a service class named HeroService with a method sayHello that takes an object with a name property and returns an object with a message property. The message should be `Hello, ${name}!`. Export this service and add it to the providers array in AppModule.
NestJS
Hint
Use @Injectable() on the service and return the greeting message using a template string.
4
Complete the gRPC server setup and run
Ensure the main.ts file imports AppModule and starts the microservice with gRPC transport as configured. The server should listen for incoming gRPC requests using the HeroService implementation.
NestJS
Hint
Combine the service and main bootstrap code to run the gRPC server.
Practice
(1/5)
1. What is the main purpose of using gRPC transport in a NestJS application?
easy
A. To enable fast and clear communication between microservices using Protocol Buffers
B. To create RESTful APIs with JSON responses
C. To manage database connections efficiently
D. To handle user authentication and authorization
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 A
Quick Check:
gRPC transport = fast microservice communication [OK]
Hint: gRPC = fast microservice communication with Protocol Buffers [OK]
Common Mistakes:
Confusing gRPC with REST APIs
Thinking gRPC manages databases
Assuming gRPC handles authentication
2. Which decorator is used in NestJS to link a method to a gRPC service method?
easy
A. @GrpcMethod
B. @GrpcClient
C. @GrpcHandler
D. @GrpcService
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 A
Quick Check:
Method to gRPC service = @GrpcMethod [OK]
Hint: Method to gRPC service uses @GrpcMethod decorator [OK]
Common Mistakes:
Using @GrpcService instead of @GrpcMethod
Confusing client decorators with method decorators
Misspelling the decorator name
3. Given this NestJS gRPC service method, what will be the output when called with { id: 5 }?
If no hero matches data.id, find returns undefined.
Step 2: Identify the error
Accessing .name on undefined causes a runtime error.
Final Answer:
Possible runtime error if hero not found -> Option C
Quick Check:
Accessing property on undefined causes error [OK]
Hint: Check if find() result can be undefined before accessing properties [OK]
Common Mistakes:
Ignoring undefined result from find()
Assuming decorator name is wrong
Thinking return type annotation is missing
5. You want to implement a streaming gRPC method in NestJS that sends multiple messages back to the client. Which approach correctly sets up the method signature and decorator?
hard
A. @GrpcMethod('ChatService', 'StreamMessages')
streamMessages(data: { userId: number }): Promise<{ message: string }> {
// implementation
}
B. @GrpcMethod('ChatService', 'StreamMessages')
streamMessages(data: { userId: number }): Observable<{ message: string }> {
// implementation
}
C. @GrpcMethod('ChatService', 'StreamMessages')
streamMessages(data: { userId: number }): { message: string } {
// implementation
}
D. @GrpcMethod('ChatService', 'StreamMessages')
streamMessages(data: { userId: number }): void {
// implementation
}
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 B
Quick Check:
Streaming gRPC method returns Observable [OK]
Hint: Streaming methods return Observable, not Promise or plain object [OK]
Common Mistakes:
Using Promise instead of Observable for streaming
Returning plain object for streaming method
Using void return type for gRPC method
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.