Skip to main content
St Louis

Back to all posts

How to Run Both Server And Client Using Tonic In Rust?

Published on
4 min read
How to Run Both Server And Client Using Tonic In Rust? image

Best Rust Development Tools to Buy in October 2025

1 Write Powerful Rust Macros

Write Powerful Rust Macros

BUY & SAVE
$46.72 $59.99
Save 22%
Write Powerful Rust Macros
2 Rust Eraser Sabitoru Medium and Fine 2-piece Set

Rust Eraser Sabitoru Medium and Fine 2-piece Set

  • EFFORTLESSLY REMOVES TOUGH RUST WITH MINIMAL SCRUBBING NEEDED!
  • SOAK FOR 10 MINUTES-NO PAINFUL EFFORT REQUIRED!
  • INCLUDES MEDIUM & FINE GRAIN FOR VERSATILE RUST REMOVAL!
BUY & SAVE
$12.80
Rust Eraser Sabitoru Medium and Fine 2-piece Set
3 CRC Evapo-Rust, Heavy-Duty Rust Remover, Reusable, Acid-Free, Non-Corrosive, Water-based, 32 oz, Removes Rust to Bare Metal

CRC Evapo-Rust, Heavy-Duty Rust Remover, Reusable, Acid-Free, Non-Corrosive, Water-based, 32 oz, Removes Rust to Bare Metal

  • EFFORTLESSLY REMOVES RUST WITHOUT SCRUBBING OR SANDING.
  • SAFE, WATER-BASED SOLUTION FOR ALL METAL TYPES.
  • EASY APPLICATION: SUBMERGE, RINSE, AND PROTECT!
BUY & SAVE
$12.99
CRC Evapo-Rust, Heavy-Duty Rust Remover, Reusable, Acid-Free, Non-Corrosive, Water-based, 32 oz, Removes Rust to Bare Metal
4 Oil Eater Overnight Rust Remover - Safe & Easy Soak for Tools, Auto Parts, Antiques, 32oz Concentrate - Makes 1-Gallon

Oil Eater Overnight Rust Remover - Safe & Easy Soak for Tools, Auto Parts, Antiques, 32oz Concentrate - Makes 1-Gallon

  • RUST-SOAK TECHNOLOGY: EFFORTLESSLY DISSOLVES RUST DOWN TO THE SURFACE.

  • SAFE & BIODEGRADABLE: RESTORES WITHOUT HARSH ACIDS, FUMES, OR ODORS.

  • COST-EFFECTIVE SOLUTION: DILUTE TO MAKE UP TO 1-GALLON OF RUST REMOVER!

BUY & SAVE
$9.18 $9.87
Save 7%
Oil Eater Overnight Rust Remover - Safe & Easy Soak for Tools, Auto Parts, Antiques, 32oz Concentrate - Makes 1-Gallon
5 Blackhat Rust: Offensive Security, Malware Development, and Ethical Hacking with the Rust Programming Language

Blackhat Rust: Offensive Security, Malware Development, and Ethical Hacking with the Rust Programming Language

BUY & SAVE
$29.50
Blackhat Rust: Offensive Security, Malware Development, and Ethical Hacking with the Rust Programming Language
6 Zerust Rust Prevention Plastabs 1" x 3" - Pack of 10 - Made in the USA

Zerust Rust Prevention Plastabs 1" x 3" - Pack of 10 - Made in the USA

  • SHIELDS TACKLE & FIREARMS FROM RUST FOR 2 YEARS!
  • LIGHTWEIGHT PLASTABS USE PATENTED ZERUST TECH.
  • EACH TAB PROTECTS A 0.6 FT RADIUS-GREAT COVERAGE!
BUY & SAVE
$8.95
Zerust Rust Prevention Plastabs 1" x 3" - Pack of 10 - Made in the USA
+
ONE MORE?

To run both a server and client using Tonic in Rust, you first need to create a new Rust project and add the Tonic crate as a dependency in your Cargo.toml file. Then, you can define your service using the tonic_build macro and implement the service trait on a struct.

For the server, you can create a new tokio runtime, bind an address, and serve your service using Tonic's server module. For the client, you can create a new tokio runtime, create a channel to connect to the server, and make RPC calls to the server using the generated client trait.

In order to run both the server and client simultaneously, you can spawn two separate async tasks for the server and client, each running on their own tokio runtime. This way, both the server and client can interact with each other over the network using Tonic's gRPC framework.

What is the difference between Tonic and Actix in Rust?

Tonic and Actix are both frameworks for building asynchronous web applications in Rust, but they have some key differences:

  1. Tonic is a gRPC framework for Rust, designed to be compatible with the gRPC protocol. It allows you to define RPC services using Protocol Buffers and generate client and server code. Actix, on the other hand, is a general-purpose web framework that supports HTTP and WebSocket protocols.
  2. Tonic is built on top of the Tokio asynchronous runtime, which is widely used in the Rust ecosystem. Actix, on the other hand, has its own asynchronous runtime that is optimized for high performance. This can result in differences in how applications are structured and how they perform.
  3. Tonic is focused on providing a simple and consistent API for building gRPC services, while Actix is more flexible and allows you to build a wide range of web applications. This means that Tonic may be a better choice if you specifically need to implement gRPC services, while Actix may be more suitable for general web development.

Overall, the choice between Tonic and Actix will depend on the specific requirements of your project and whether you need to use gRPC or prefer a more general-purpose web framework.

How to test a Tonic server in Rust?

To test a Tonic server in Rust, you can use the built-in testing framework provided by the Rust language, as well as the tonic-test crate. Here are some steps to follow:

  1. Create a new Rust project or add Tonic as a dependency to an existing project. You can do this by adding the following lines to your Cargo.toml file:

[dependencies] tonic = "0.4" tonic-test = "0.4"

  1. Write your Tonic server code and define your service using gRPC. Make sure to create a function to handle the requests from your client.
  2. Write your test cases using the Rust testing framework. You can use the tonic-test crate to create mock gRPC clients and servers for testing. Here is an example of a test case for a Tonic server:

#[cfg(test)] mod tests { use tonic::transport::Server; use tonic::Response; use tonic::Request; use tonic::transport::Channel; use tonic_test::client_server::*; use crate::server::MyServiceServer;

#\[tokio::test\]
async fn test\_my\_service() {
    // Create a mock server
    let service = MyServiceServer::new(MyServiceImpl::default());
    let addr = "\[::1\]:50051".parse().unwrap();
    let server = Server::builder()
        .add\_service(service)
        .serve(addr);
    
    // Create a mock client
    let channel = Channel::from\_static("http://\[::1\]:50051")
        .connect()
        .await
        .unwrap();
    let client = MyServiceClient::new(channel);
    
    // Make a request to the server
    let request = Request::new(MyRequest { /\* request fields here \*/ });
    let response = client.my\_method(request).await.unwrap();
    
    // Assert the response
    assert\_eq!(response.get\_ref(). /\* response fields here \*/, /\* expected value \*/);
}

}

  1. To run the tests, use the following command:

cargo test

This will compile and run your test cases, providing you with feedback on the success of your Tonic server testing.

How to use Tonic with Docker in Rust?

To use Tonic with Docker in Rust, you can follow these steps:

  1. Create a new Rust project using Cargo:

cargo new my_project

  1. Add Tonic to your project's dependencies in the Cargo.toml file:

[dependencies] tonic = "0.5"

  1. Write your Tonic RPC service implementation in the src/main.rs file.
  2. Create a Dockerfile in the root of your project directory:

FROM rust:latest as builder

WORKDIR /usr/src/my_project COPY . .

RUN cargo build --release

FROM debian:buster-slim

COPY --from=builder /usr/src/my_project/target/release/my_project /usr/local/bin/my_project

CMD ["/usr/local/bin/my_project"]

  1. Build your Docker image using the Dockerfile:

docker build -t my_project .

  1. Run your Docker container:

docker run my_project

Your Tonic server should now be running inside a Docker container.