Performance: gRPC transport
This affects the speed and efficiency of communication between frontend and backend services, impacting interaction responsiveness and load times.
import { Transport, ClientsModule } from '@nestjs/microservices'; @Module({ imports: [ ClientsModule.register([{ name: 'SERVICE', transport: Transport.GRPC, options: { package: 'service', protoPath: 'service.proto' } }]), ], }) export class AppModule {}
import { Transport, ClientsModule } from '@nestjs/microservices'; @Module({ imports: [ ClientsModule.register([{ name: 'SERVICE', transport: Transport.HTTP }]), ], }) export class AppModule {}
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| HTTP/REST transport | N/A | N/A | N/A | [!] OK |
| gRPC transport | N/A | N/A | N/A | [OK] Good |
gRPC transport in a NestJS application?@GrpcMethod.{ 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' };
}{ id: 5 } and searches the heroes array for a hero with id 5. @GrpcMethod('HeroService', 'FindOne')
findOne(data: { id: number }): { name: string } {
const heroes = [{ id: 1, name: 'Superman' }];
return heroes.find(hero => hero.id === data.id).name;
}data.id, find returns undefined..name on undefined causes a runtime error.Pass the test and this pattern moves unknown