gRPC is a Remote Procedure Call (RPC) framework that can run in any environment. It can connect services in and across data centers with pluggable support for load balancing, tracing, health checking and authentication. It is also applicable in last mile of distributed computing to connect devices, mobile applications and browsers to backend services.
This page shows how to use gRPC in Foal. It is based on the official gRPC tutorial.
First you need to install some additional dependencies.
Then update your
package.json so that your build scripts will correctly copy your
.proto files into the
spec.proto file and save it to
The next step is to create a service that will instantiate the gRPC server.
Finally, inject the service in the application.
Using callbacks in the grpc services can be quite tedious. To convert methods into functions that use promises, you can use the decorator below.
The implementation above do not use Foal regular controllers. This has two consequences:
- hooks cannot be used,
- and error-handling is entirely managed by the gRPC server. The
AppController.handleErrorcannot be used.