Quick Start
Prerequisites
Your application should be using grpc-web.
Ensure you have the Service Client stubs generated from protoc.
Installation
To install grpcExpress using npm
npm i @grpcexpress/grpcexpress
Initial Configuration
No additional configuration is needed to start using the library.
You can optionally set the default cache expiration time in ms and the in-memory cache size in bytes.
const Client = grpcExpressClient(
ServiceClient,
cacheDuration = 60 * 1000,
cacheSize = 1024 * 1024)
Default Settings
The default expiration time is 10 minutes.
The default in-memory cache is 100MB
Basic Usage
To cache unary method calls
Import { grpcExpressClient } from '@grpcexpress/grpcexpress'
// obtain a client factory by passing in the Service Client from grpc stub
Const Client = grpcExpressClient(ServiceClient)
// instantiate a service client
const client = new Client('http://localhost:8080');
// Once integrated, you can utilize the client just as you would with the original client.
// The caching mechanism is activated by default, optimizing your calls right out of the box.
// Responses are automatically cached
Const response = await client.myMethod(message)
Custom React Hook
To install the React hook for grpcExpress
npm i @grpcexpress/usegrpcexpress
Then use the hook like below
const { isLoading, isError, data, error } = useGrpcExpress(client.myMethod, message)