This is an example service to get comfortable with building a microservice based on the gRPC protocol and Protocol Buffers.
As an alternative to the common JSON format, Protocol Buffers are a condensed data format which support gRPC calls. Compared to REST(ful) APIs which uses HTTP 1.1, the gRPC protocol utilizes faster and safer HTTP 2 calls. While REST APIs provide CRUD (Create, Retrieve, Update, Delete) processes, gRPC is asynchronous and more flexible by providing four different options of streaming data. In general, it is possible to make unary gRPC calls, server side streaming, client side streaming and bidirectional streaming.
The compilation of Protocol Buffers can be conducted statically by pre-processing or dynamically by loading and parsing the .proto
file at runtime.
protoc -I=. ./proto/weather.proto \
--js_out=import_style=commonjs,binary:./server \
--grpc_out=./server \
--plugin=protoc-gen-grpc=`which grpc_tools_node_protoc_plugin`
While the statical compilation is executed as above, the dynamic conversion requires some additional setup and a package that can be installed with npm install @grpc/proto-loader
.
- Install Dependencies with
npm install
. - Compile Proto file by
npm run proto
. - Generate SSL Certificates for encrypted communication by
npm run gen:certs
. - Start server by
npm run server
. - Start client by
npm run client
.
- Official gRPC Node Documentation
- Gonzalo Bahamodez and his gRPC SSL Demo
- Official gRPC Java Documentation
- Official Protobuf Documentation