This examples shows how to use the proto compiler, ts-protoc-gen
& @grpc/grpc-js
to build a fully typed gRPC CLI application that runs on Node.js.
The proto compiler and ts-protoc-gen
compiler plugin are used to generate JavaScript & TypeScript files from the proto definitions.
google-protobuf
& @grpc/grpc-js
are used at runtime.
- package.json - Dependencies and node build scripts
- compile-proto.sh - The proto compiler script
- proto/ - Protobuf definitions and generated types
- server.ts - The grpc server
- client.ts - The grpc client
Install dependencies:
npm install
# Apple M1 users should install for x64 due to grpc-tools not supporting arm64
npm install --target_arch=x64
Use proto compiler and ts-protoc-gen
to generate the TypeScript files:
./compile-proto.sh
This is aliased as a npm script:
npm run build:proto
This simple app demonstrates the different gRPC calls you can perform.
First generate the types and build the application files:
npm run build
Start the server:
npm run start:server
Now run the client by specifying which example you want to run:
npm run start:client -- --unary
npm run start:client -- --server-streaming
npm run start:client -- --client-streaming
npm run start:client -- --bidi-streaming