The main purpose of this repository is to demonstrate how to implement different gRPC patterns in TypeScript, with examples of unary, server streaming, client streaming, and bidirectional streaming communications.
The project implements a chat room system to showcase different gRPC patterns:
- Unary RPC: Room registration (
RegisterToRoom
) - Server Streaming: Chat monitoring (
MonitorChatRoom
) - Client Streaming: News updates (
SendNewsUpdate
) - Bidirectional Streaming: Real-time chat (
Chat
)
- Node.js (v18 or newer)
- pnpm (or npm/yarn)
- Protocol Buffers compiler
- Clone the repository
- Install dependencies:
pnpm install
The project includes a Makefile
for generating Protocol Buffer code. Main commands:
make gen
: Generate Protocol Buffer code (alias forgenerate
)make clean
: Clean generated codemake regen
: Regenerate all Protocol Buffer code (alias forregenerate
)
The application uses the config
package for configuration management. Create a config file based on the sample provided.
- Copy the sample config:
cp config/default.sample.yml config/default.yml
- Update the configuration values as needed
pnpm start
For development with hot reload:
pnpm start:dev
pnpm build && node dist/chat-monitoring-client
pnpm build && node dist/news-update-client
pnpm build && node dist/chat-room-client
The following commands can be used to exit the chat:
exit
\exit
quit
\quit
\q
- grpc-tools
- grpc_tools_node_protoc_ts
- google-protobuf (the protobuf runtime library.)
- @grpc/grpc-js (pure JavaScript gRPC client, replacement for the grpc (deprecated) library)
The grpc-tools
module allows us to actually generate the static code that we need. Installing this module globally (not recommended anymore) makes the grpc_tools_node_protoc_plugin
available for us, which we will use to generate JS output from our proto files.
This library also exports the grpc_tools_node_protoc
executable, which accepts all of the same arguments as protoc
itself. For use with Node.js, you most likely want to use the CommonJS-style imports. We will be using the local executable instead of globally installing this library.
NOTE: The executables installed by node libraries get installed inside of the node_modules/.bin
directory.
The grpc_tools_node_protoc
executable automatically includes the Node gRPC plugin, so it also accepts the --grpc_out=[option:]path argument.
We will add a script in our package.json file (or better yet a Makefile
) to make use of the grpc_tools_node_protoc
executable to generate JS code for us.
$ grpc_tools_node_protoc \
--js_out=import_style=commonjs,binary:./src/pb \
--grpc_out=grpc_js:./src/pb \
-I proto proto/\*.proto
This library can be used to generate corresponding TypeScript type definition files according to JS codes generated by grpc_tools_node_protoc
.
We will add another script to make use of this library to generate TypeScript type definition files for us.
$ grpc_tools_node_protoc \
--plugin=protoc-gen-ts=node_modules/.bin/protoc-gen-ts \
--ts_out=grpc_js:./src/pb \
-I proto proto/\*.proto
PROTOC=node_modules/.bin/grpc_tools_node_protoc
PROTOC_GEN_TS=node_modules/.bin/protoc-gen-ts
PROTO_DIR=protos
PROTO_OUT_DIR=./src/proto-out
generate:
mkdir -p $(PROTO_OUT_DIR)
$(PROTOC) \
--js_out=import_style=commonjs,binary:./$(PROTO_OUT_DIR) \
--grpc_out=grpc_js:./$(PROTO_OUT_DIR) \
-I $(PROTO_DIR) $(PROTO_DIR)/*.proto
$(PROTOC) \
--plugin=protoc-gen-ts=$(PROTOC_GEN_TS) \
--ts_out=grpc_js:./$(PROTO_OUT_DIR) \
-I $(PROTO_DIR) $(PROTO_DIR)/*.proto
gen: generate # alias for 'generate'
clean:
rm -rf $(PROTO_OUT_DIR)/*
regenerate: clean generate # alias for 'clean' and 'generate'
regen: regenerate # alias for 'regenerate'
The chat room service implements four types of gRPC patterns:
- Unary RPC: Room registration
- Server Streaming: Chat room monitoring
- Client Streaming: News updates
- Bidirectional Streaming: Real-time chat
The application uses an in-memory message queue system to manage messages between different chat rooms and users. The queue system handles:
- Per-user message queues
- Room-based message broadcasting
- Global chat monitoring
For development, the project includes:
- TypeScript configuration
- Nodemon for hot reloading
- Path aliases for cleaner imports
- Server reflection for debugging
- Setting up gRPC with TypeScript
- Generating TypeScript definitions from protobuf files
- Implementing different gRPC patterns
- Handling streams and events
- Type-safe gRPC communications