This repository provides an example implementation of gRPC using TypeScript. It is part of the gRPC Introduction blog series. For a detailed explanation, please visit the blog:
👉 gRPC Introduction (Chapter 2)
This project demonstrates how to:
- Define gRPC services and messages using Protocol Buffers.
- Generate TypeScript interfaces and gRPC code using
ts-proto
. - Implement all four types of gRPC calls:
- Unary RPC
- Server-Side Streaming RPC
- Client-Side Streaming RPC
- Bidirectional Streaming RPC
The example includes both server-side and client-side implementations for each type of gRPC call.
Before running the project, ensure you have the following tools installed:
- Node.js (v14 or later)
- npm or Yarn
- protoc (Protocol Buffers Compiler)
Follow these steps to set up the project:
-
Clone the Repository:
git clone https://github.com/qstommyshu/grpc-ts-example.git cd grpc-ts-example
-
Install Dependencies:
Use Yarn to install the required dependencies:
yarn install
-
Generate TypeScript Code from Protobuf:
Run the following script to generate TypeScript interfaces and gRPC code:
yarn run proto:build
-
Run the gRPC Server:
Start the gRPC server by running:
yarn run server
-
Test gRPC Clients:
Use the following commands to test each RPC example:
- Unary RPC:
yarn run client:unary
- Server-Side Streaming RPC:
yarn run client:server-stream
- Client-Side Streaming RPC:
yarn run client:client-stream
- Bidirectional Streaming RPC:
yarn run client:bidirectional
The repository has the following structure:
grpc-ts-example/
│
├── generated/ # Code generated by `protoc` with ts-proto plugin
│ └── rpc_example.ts # Generated TypeScript interfaces and service definitions
│
├── rpc_example.proto # Protobuf file defining the gRPC service and messages
│
├── src/ # Source code for server and client implementations
│ ├── server.ts # gRPC server implementation
│ └── client/ # Client implementations for each RPC type
│ ├── unaryExample.ts # Unary RPC client
│ ├── serverStreamExample.ts # Server-side streaming RPC client
│ ├── clientStreamExample.ts # Client-side streaming RPC client
│ └── bidirectionalExample.ts # Bidirectional streaming RPC client
│
└── package.json # Project configuration and scripts
Here is an overview of the four types of gRPC calls implemented in this example:
-
Unary RPC:
- The client sends a single request and receives a single response.
- Example: A client sends a message, and the server responds with a confirmation.
-
Server-Side Streaming RPC:
- The client sends a single request, and the server streams multiple responses.
- Example: A client requests a large file, and the server streams the file in chunks.
-
Client-Side Streaming RPC:
- The client streams multiple requests, and the server sends a single response after processing.
- Example: A client uploads file chunks to the server, and the server responds with a success status.
-
Bidirectional Streaming RPC:
- Both the client and server stream data to each other simultaneously.
- Example: A chat application where both sides send and receive messages in real time.
- Blog Post: gRPC Introduction (Chapter 2)
- Official gRPC Documentation: grpc.io
- Protocol Buffers: Protocol Buffers Documentation
This project is licensed under the Apache 2.0 License. See the LICENSE file for details.
Written by qstommyshu