grpc-base-client
v2.3.0
Published
This project is a Library base for construct gRPC clients
Downloads
179
Readme
- This library auto-apply promise into Unary calls
- This library implements a consistent Connection Pool with a Round-Robin strategy auto manageable for its instance or new instances;
- It's supports gRPC and gRPC-js implementations;
How to Install
npm i @codibre/grpc-base-client
How to Use
import { Client as gRPCCLient } from '@codibre/grpc-base-client';
interface Health {
Check(props: { service: string }): Promise<any>;
}
const grpcCLient = new gRPCCLient<Health>({
namespace: 'abc.def',
protoFile: 'health-check.proto',
url: 'test.service',
maxConnections: 2,
service: 'Health',
secure: true,
});
await grpcCLient.getInstance().Check({service: 'foo'}); // { status: 'SERVING' }
How to retrieve client through reflection
Is your GRPC servers implements GRPC Reflection Protobol, then you don't need to have a copy of the proto client side and can just get it using it! This package implements a method to create the client using this feature that can be easily used like this:
import { Client as gRPCCLient } from '@codibre/grpc-base-client';
interface Health {
Check(props: { service: string }): Promise<any>;
}
const grpcCLient = gRPCCLient.getByReflection<Health>({
namespace: 'abc.def',
url: 'test.service',
maxConnections: 2,
service: 'Health',
secure: true,
});
await grpcCLient.getInstance().Check({service: 'foo'}); // { status: 'SERVING' }
Notice that you don't inform the protoFile in this call, as it is not needed. Also, this method don't offers a legacy library switch, as it'll only work with the new @grpc/grpc-js library.
If you want to find out how to implement it on your server, it really pretty easy! Just take a look at the official implementation for NodeJs: @grpc/reflection. If you're using NestJs, another option is nestjs-grpc-reflection, although the official one is preatty straight forward to use alongside with nestjs too.
To use getByReflection, you need to install two optional libraries:
License
Licensed under MIT.