@betsys-nestjs/grpc
v2.0.0
Published
Library for bootstrapping grpc hybrid application and health checking of grpc
Downloads
8
Maintainers
Keywords
Readme
gRPC library
This library is responsible bootstraping GRPC application and providing GRPC client for health checking.
Dependencies
| Package | Version | | --------------------- | ------- | | @grpc/grpc-js | ^1.8.0 | | @nestjs/common | ^10.0.0 | | @nestjs/microservices | ^10.0.0 | | @nestjs/core | ^10.0.0 | | @nestjs/terminus | ^10.0.0 | | reflect-metadata | ^0.1.13 | | rxjs | ^7.8.0 |
Usage
To start using this library simply import GrpcBuilder
in main.ts as an entry point of Nest application.
const {app, microservice} = await GrpcBuilder.createGrpcApplication(
TestModule,
{
options: {
url: '0.0.0.0:<port>',
package: ['<package>'],
protoPath: ['<protoPath>'],
loader: {
includeDirs: [
'<pathToProtobuff>'
],
},
},
health: true,
},
)
Then if you enable health checking for the GRPC
you want to use GrpcHealthClientModule
to initialize GRPC client for calling health check endpoint.
At the place where you want to use GRPC client simply do:
GrpcHealthClientModule.register(port, directoryOfProtofile)
You always need to define port, that would match the port
of running
GRPC server, and optionally you can define path of proto file with second parameter of register
method.
Then you can inject service to any module and use it like this to call the actual health check method:
class HealthIndicator {
constructor(private readonly grpcHealthClientService: GrpcHealthClientService) {
}
async isHealthy() {
await this.grpcHealthClientService.check()
}
}