grpc-web — Using gRPC in your front end application
At StackPulse we use gRPC as our one and only synchronous communication protocol. Microservices communicate one with other using gRPC, our external api is exposed via gRPC and also our frontend application (that is written using VueJS) uses the gRPC protocol to communicate with our backend services.
One of the main strengths of gRPC is the community and the different languages support. Given some proto files you can generate a server and a client for most programming languages.
In this article, I will explain how to use communicate with your gRPC backend using the great grpc-web OSS project.
A quick reminder of our architecture
As you may remember from my previous blog posts we, at StackPulse, are using microservice architecture. Initially each microservice had an external and internal api endpoint.
After a working that way for several months we have realized that it doesn’t work as well as we’ve imagined. We’ve then decided to adopt the API Gateway/Backend For Frontend approach.
I will publish a different blog post about this change in the future, however the main pain point was data consolidation across several microservices. The dreaded JOINs over gRPC.
Envoy has built in support for this type of proxy.
Here is an example of the grpc-web proxy implementation in Golang. We use a similar one at StackPulse.
Building gRPC-Web clients
We generate gRPC clients for all our external APIs during the CI process. In case of gRPC-Web client an npm package is generated and it is published to github package repository.
npm install command.
Sample client/server example
Our proto interface
Its a really simple service. You sent it
GetCurrentTimeRequest and you get
GetCurrentTimeResponse which contains the textual representation of
Generating clients and servers
protoc-gen-grpc-web plugin. You can get it here or like me use the pre-baked docker image
jfbrandhorst/grpc-web-generators that contains all the tools needed to work with grpc-web.
docker run \
-v `pwd`/api:/api \
-v `pwd`/time/goclient:/goclient \
-v `pwd`/frontend/src/jsclient:/jsclient \
protoc -I /api \
It will put the Go clients in
A really basic Go server which just listens on
0.0.0.0:8080. It implements the
TimeServiceServer interfaceand returns
time.Now().String() for each request.
Using grpc-web in frontend is pretty simple.
This is pretty straight forward. I recommend enabling the grpc-web chrome extension support. It’s a great way to debug your traffic. You can find more details on the extension here.
Like I previously mentioned, grpc-web needs a proxy to translate into gRPC. Envoy has native support for this.
The following configuration example for Envoy does exactly that.
I hope this article will help you easily dive into grpc-web. It’s great technology, especially if you are already using gRPC everywhere.
We are using this with great success in our frontend application.
You can find the source code for this article here.