This repository provides an example of using gRPC with Protobuf in TypeScript and JavaScript for a simple health check service.
The codebase demonstrates how to set up the gRPC server in Go, and how to create TypeScript and JavaScript clients that can interact with it.
There's another example in this repository, where Protobuf is used to serialize messages for use with an MQTT broker. Check out the mqtt
branch
The repository is structured as follows:
proto/health/v1/health.proto
: This is the protocol buffer definition for the health check service.client-server-ts
: This directory contains the TypeScript client and server code.client.ts
andserver.ts
are the main files to look at for seeing how to use the generated code to create a gRPC server and client.
client-js
: Similar to the TypeScript directory, this contains a JavaScript client for the health check service.grpc.js
shows how to use the generated JavaScript code to create a gRPC client.
main.go
andhandlers/health_handler.go
: These files set up a simple gRPC server in Go using the protocol buffer definitions.Makefile
: This file provides convenient commands for building and running the project.
Before running the examples, ensure that you have Node.js, Go, and buf
installed on your machine.
For running the Go server:
- Run
make serve
from the root directory to start the Go gRPC server.
For the TypeScript client:
- Navigate to the
client-ts
directory. - Run
buf generate buf.build/djungermann/grpc-example
. This fetches the proto files from the remote registry. - Run
npm install
to install the necessary dependencies. - Make a request to a server using
npx tsx grpc.ts
- Make a request to a server using
npx tsx http.ts
For the JavaScript client:
- Navigate to the
client-js
directory. - Run
buf generate buf.build/djungermann/grpc-example
. This fetches the proto files from the remote registry. - Run
npm install
to install the necessary dependencies. - Run
node grpc.js
to make a request to a server via gRPC. - Run
node http.js
to make a request to a server via HTTP.
In each case, the client will send a health check request to the server and log the response.
This way, you can run the Go service, and make requests from both Typescript and Javascript. If you update the proto file and push that to Buf, you can try out how to propagate changes, and then implement them in the various examples.
It's also possible to use connect-es to make the HTTP request, but I'll leave that up to the reader to implement
The TypeScript and JavaScript clients demonstrate how to use the protocol buffer messages and service stubs generated from the .proto
files to make requests to a gRPC server.
Contributions are welcome! Please feel free to submit a pull request.
This project is open source under the MIT license. See the LICENSE file for details.