This project demonstrates a simple gRPC communication setup between a Rust server (using the Tonic framework) and a Node.js client.
It features a Greeter
service with two RPC methods:
SayHello
: The client sends a name, and the server responds with a personalized greeting.ExchangeDataTypes
: The client sends a message containing various Protobuf scalar data types, and the server echoes them back with a confirmation. This is useful for understanding how different data types are handled in gRPC.
grpc_project/
├── proto/
│ └── greeter.proto # gRPC service and message definitions
├── rust_server/
│ ├── Cargo.toml # Rust project manifest
│ ├── build.rs # Rust build script for compiling .proto
│ └── src/
│ └── main.rs # Rust gRPC server implementation
└── node_client/
├── package.json # Node.js project manifest
└── client.js # Node.js gRPC client implementation
- Rust: Install Rust and Cargo (Rust's package manager and build system). Follow the instructions at rust-lang.org.
- Node.js and npm: Install Node.js and npm (Node.js package manager). Follow the instructions at nodejs.org.
- Protocol Buffer Compiler (
protoc
): Whiletonic-build
in Rust and@grpc/proto-loader
in Node.js handle most of the heavy lifting, havingprotoc
installed can be useful for manual compilation or validation. It's often a dependency for gRPC tools in various languages. Installation instructions can be found on the Protocol Buffers GitHub page.
If you're working from a Git repository:
git clone <repository-url>
cd <repository-directory>
This file defines the contract between the server and the client. Any changes to the service methods or message structures should be made here. After modifying this file, you'll need to recompile the Rust server to generate the updated Rust code.
Open a terminal window:
cd rust_server
cargo run
The first time you run this, Cargo will download dependencies and compile the project, including generating Rust code from greeter.proto
(due to build.rs
).
You should see output similar to:
GreeterServer listening on https://0.0.0.0:50051
The server will now be running and waiting for requests on port 50051
.
Open a new terminal window (leave the server running in the first one):
cd node_client
npm install
To run the client with default values:
npm start
Or, to pass a custom name for the SayHello
RPC:
node client.js "Your Name"
You should see output in the client's terminal showing the responses from the server for both SayHello
and ExchangeDataTypes
RPC calls.
Example client output:
Greeting from server for "Node.js User (default)": Hello Node.js User (default)!
Response from ExchangeDataTypes:
Double: 3.14159
Float: 2.718
... (other data types) ...
Confirmation: Server received and echoed data for string: Hello from Node.js client with all data types!
And in the Rust server's terminal, you'll see logs indicating received requests.
- Add more RPC methods to
greeter.proto
. - Experiment with different Protobuf message types (e.g., enums, nested messages,
repeated
fields,map
fields). - Implement streaming RPCs (client-streaming, server-streaming, bidirectional-streaming).
- Add error handling and status codes.
- Explore gRPC interceptors/middleware in Tonic and
@grpc/grpc-js
. - Secure your gRPC communication using TLS.
This `README.md` covers the basics of your project. You can, of course, expand on it with more details specific to your project's goals or any advanced features you implement.