This document will show you how to build a browser/server application with Vert.x and gRPC Web.
The application involves a client, the browser, and a Vert.x server:
-
the user types a name in a text field and clicks the send button
-
the browser sends the name to the server using the gRPC Web protocol
-
the server replies with a greeting
-
the browser displays the greeting
On the server side, you will create a Vert.x gRPC server service that:
-
implements a gRPC server stub
-
configures an HTTP server replying to both gRPC Web and static file requests
On the client side, you will create a web page that uses the gRPC Web Javascript client.
-
A text editor or an IDE
-
Java 17 or higher
-
The
protocJavascript plugin -
The
protocgRPC Web plugin
The protoc Javascript and gRPC Web plugin files must be installed somewhere on your PATH and be executable.
| Tip | On several Linux-like systems, you can install the plugins like this: mkdir --parents "$HOME/.local/bin" curl -sL https://github.com/protocolbuffers/protobuf-javascript/releases/download/v3.21.4/protobuf-javascript-3.21.4-linux-x86_64.tar.gz | tar xzfO - bin/protoc-gen-js > "$HOME/.local/bin/protoc-gen-js" chmod u+x "$HOME/.local/bin/protoc-gen-js" curl -sL https://github.com/grpc/grpc-web/releases/download/1.5.0/protoc-gen-grpc-web-1.5.0-linux-x86_64 > "$HOME/.local/bin/protoc-gen-grpc-web" chmod u+x "$HOME/.local/bin/protoc-gen-grpc-web" |
You don’t have to install protoc or the Java plugin. They will be managed by a Maven plugin.
The gRPC Greeter service consists in a single SayHello rpc method. The HelloRequest message contains the name sent by the client. The HelloReply message contains the greeting generated by the server.
From the service definition, several files must be generated:
-
Java message and server classes
-
Javascript files
-
gRPC Web specific (Javascript) files.
The protoc invocation is managed with the protobuf-maven-plugin.
We need some dependencies for the project to compile:
The server side code fits in a single ServerVerticle class.
First, the gRPC server stub implementation.
There is nothing specific to gRPC Web here.
| Note | GrpcIoServer enables the gRPC Web protocol support by default. |
Then we have to configure a Vert.x Web Router to accept both gRPC Web and static file requests.
