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 December 2025

1 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

  • EASY 12-HOUR SOAK: RESTORE RUSTED TOOLS WITH ZERO SCRUBBING NEEDED!

  • BIODEGRADABLE FORMULA: SAFE RUST REMOVAL WITHOUT HARSH CHEMICALS!

  • UNBEATABLE VALUE: DILUTE TO MAKE 1 GALLON - REUSE UNTIL EFFECTIVE!

BUY & SAVE
$9.18
Oil Eater Overnight Rust Remover - Safe & Easy Soak for Tools, Auto Parts, Antiques, 32oz Concentrate - Makes 1-Gallon
2 Rust Standard Library Cookbook: Over 75 recipes to leverage the power of Rust

Rust Standard Library Cookbook: Over 75 recipes to leverage the power of Rust

BUY & SAVE
$22.26 $48.99
Save 55%
Rust Standard Library Cookbook: Over 75 recipes to leverage the power of Rust
3 Rust Eraser, Premium Rust Remover for Cast Iron Pans, Knives & Tools, Hard Anodized Cookware and Other Pots, Remove Rust from Any Metallic Surface - 1 Pack

Rust Eraser, Premium Rust Remover for Cast Iron Pans, Knives & Tools, Hard Anodized Cookware and Other Pots, Remove Rust from Any Metallic Surface - 1 Pack

  • NON-TOXIC & SAFE: PROTECT YOUR COOKWARE WITHOUT HARMFUL CHEMICALS!

  • EFFECTIVE & GENTLE: REMOVE RUST WHILE PRESERVING YOUR COOKWARE'S FINISH.

  • VERSATILE & LONG-LASTING: PERFECT FOR ALL CAST IRON ITEMS-LASTS FOR AGES!

BUY & SAVE
$9.88
Rust Eraser, Premium Rust Remover for Cast Iron Pans, Knives & Tools, Hard Anodized Cookware and Other Pots, Remove Rust from Any Metallic Surface - 1 Pack
4 Write Powerful Rust Macros

Write Powerful Rust Macros

BUY & SAVE
$46.17 $59.99
Save 23%
Write Powerful Rust Macros
5 Zero To Production In Rust: An introduction to backend development

Zero To Production In Rust: An introduction to backend development

BUY & SAVE
$49.99
Zero To Production In Rust: An introduction to backend development
6 Rocket TT Multi-Purpose Lubricant Spray 15.2 fl oz - Penetrating Oil & Anti-Corrosion Spray for Rusted Bolts, Tools, Bikes & Garden Equipment, Water-Resistant Protective Coating

Rocket TT Multi-Purpose Lubricant Spray 15.2 fl oz - Penetrating Oil & Anti-Corrosion Spray for Rusted Bolts, Tools, Bikes & Garden Equipment, Water-Resistant Protective Coating

  • ALL-IN-ONE SOLUTION FOR RUST, SQUEAKS, AND STICKY LOCKS!

  • LONG-LASTING PROTECTIVE COATING RESISTS MOISTURE AND CORROSION.

  • EASY APPLICATION WITH PRECISE CONTROL FOR ANY SURFACE.

BUY & SAVE
$8.48 $9.29
Save 9%
Rocket TT Multi-Purpose Lubricant Spray 15.2 fl oz - Penetrating Oil & Anti-Corrosion Spray for Rusted Bolts, Tools, Bikes & Garden Equipment, Water-Resistant Protective Coating
+
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.