At Forest Giant we use gRPC for many of our projects. gRPC utilizes protocol buffers to communicate the interface of the services and then has a compiler to generate source code in one of a variety of supported languages. Our main focus is the language Go but we also utilize Node.js.
When defining services with gRPC we like to create APIs that encapsulate the gRPC client code. These APIs streamline the process of creating client applications. When researching using gRPC with Node.js we found that the most documented method of using protobufs in Node.js is to load the protobuf file into the gRPC library at runtime. Our preference is to generate the code and for the API to remove the runtime dependency on the protobuf file.
We created a GitHub Gist that has a template for how to use the protoc compiler to generate the Go and Node.js file. Feel free to star our work on GitHub or follow the guide below.
You can use the flag
-proto_path to specify the path to your protobuffer. The next flag specifies the language to compile to. The Go command is fairly well documented but the Node.js documents seem to only show how to dynamically load a proto file. This is how you generate a compiled file for use in your Node.js project.
protoc -proto_path=$SRC_PATH --go_out=plugins=grpc:$DEST_PATH $SRC_PATH/service.proto
protoc -proto_path=$SRC_PATH --js_out=import_style=commonjs,binary:$DEST_PATH --grpc_out=./ --plugin=protoc-gen-grpc=node_modules/grpc-tools/bin/grpc_node_plugin $SRC_PATH/service.proto