Skip to content

qstommyshu/grpc-ts-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gRPC-TS-Example

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)


Overview

This project demonstrates how to:

  1. Define gRPC services and messages using Protocol Buffers.
  2. Generate TypeScript interfaces and gRPC code using ts-proto.
  3. 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.


Prerequisites

Before running the project, ensure you have the following tools installed:

  • Node.js (v14 or later)
  • npm or Yarn
  • protoc (Protocol Buffers Compiler)

Setup Instructions

Follow these steps to set up the project:

  1. Clone the Repository:

    git clone https://github.com/qstommyshu/grpc-ts-example.git
    cd grpc-ts-example
  2. Install Dependencies:

    Use Yarn to install the required dependencies:

    yarn install
  3. Generate TypeScript Code from Protobuf:

    Run the following script to generate TypeScript interfaces and gRPC code:

    yarn run proto:build
  4. Run the gRPC Server:

    Start the gRPC server by running:

    yarn run server
    
  5. 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
    

Project Structure

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

RPC Examples

Here is an overview of the four types of gRPC calls implemented in this example:

  1. 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.
  2. 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.
  3. 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.
  4. 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.

Resources

License

This project is licensed under the Apache 2.0 License. See the LICENSE file for details.

Author

Written by qstommyshu

Releases

No releases published

Packages

No packages published