0
0
NestJSframework~10 mins

gRPC transport in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - gRPC transport
Define Proto File
Generate gRPC Service Interfaces
Create NestJS gRPC Microservice
Implement Service Handlers
Start Microservice
Client Sends gRPC Request
Server Processes Request
Server Sends Response
Client Receives Response
This flow shows how NestJS uses gRPC transport: define proto, generate interfaces, create microservice, implement handlers, start service, then client-server communication.
Execution Sample
NestJS
import { Controller } from '@nestjs/common';
import { GrpcMethod } from '@nestjs/microservices';

@Controller()
export class HeroService {
  @GrpcMethod('HeroService', 'FindOne')
  findOne(data: { id: number }) {
    return { id: data.id, name: 'Hero' + data.id };
  }
}
This NestJS service listens for gRPC calls to 'FindOne' method and returns a hero object with id and name.
Execution Table
StepActionInputInternal StateOutput
1Receive gRPC request{ id: 1 }Waiting for requestNone
2Invoke findOne handler{ id: 1 }Processing request with id=1None
3Create response object{ id: 1 }Response = { id: 1, name: 'Hero1' }None
4Send response backResponse objectResponse sent{ id: 1, name: 'Hero1' }
5Client receives responseResponse objectClient processes response{ id: 1, name: 'Hero1' }
6EndNo inputIdleNo further output
💡 No more requests; microservice continues listening for new calls.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
dataundefined{ id: 1 }{ id: 1 }{ id: 1 }undefined
responseundefinedundefined{ id: 1, name: 'Hero1' }{ id: 1, name: 'Hero1' }undefined
Key Moments - 2 Insights
Why does the findOne method receive an object with id instead of separate parameters?
Because gRPC methods receive a single object representing the request message, matching the proto definition. See execution_table step 2 where data is { id: 1 }.
How does NestJS know which method to call for a gRPC request?
The @GrpcMethod decorator links the proto service and method name to the handler. In the code, @GrpcMethod('HeroService', 'FindOne') tells NestJS to call findOne for that gRPC method.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'response' after Step 3?
A{ id: 1, name: 'Hero1' }
Bundefined
C{ id: 1 }
Dnull
💡 Hint
Check the 'Internal State' column at Step 3 in execution_table.
At which step does the server send the response back to the client?
AStep 2
BStep 4
CStep 5
DStep 3
💡 Hint
Look for 'Send response back' action in execution_table.
If the client sends { id: 5 } instead, what will be the 'name' in the response?
A'Hero1'
B'Hero'
C'Hero5'
Dundefined
💡 Hint
See the code in execution_sample where name is 'Hero' + data.id.
Concept Snapshot
NestJS gRPC transport uses proto files to define services.
Use @GrpcMethod to link proto methods to handlers.
Handlers receive request objects and return response objects.
Microservice listens and processes gRPC calls asynchronously.
Client-server communicate via defined proto messages.
Full Transcript
This visual execution shows how NestJS handles gRPC transport. First, a proto file defines the service and messages. NestJS generates interfaces and creates a microservice. The service class uses @GrpcMethod to handle specific gRPC calls. When a client sends a request like { id: 1 }, the findOne method receives it as an object. It processes and returns a response object with id and name. The microservice sends this back to the client. Variables like 'data' and 'response' change during execution steps. Key points include how NestJS maps proto methods to handlers and how request data is structured. The quiz tests understanding of response values and flow steps.