0

I’m trying to write a client to communicate with my grpc server written with tonic using Rust, but I’m having trouble understanding where to define and connect to the client, thus getting errors with my import statement. I’ve followed several tutorials and am having trouble finding information on how to create and import a client. My error is:

error[E0432]: unresolved import `user::client`
 --> user/src/client.rs:2:36
  |
2 | use user::client::{UserClient};
  |                                    ^^^^^^ could not find `client` in `user`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0432`.
error: could not compile `user`

In my proto.rs file:

syntax = "proto3";

package user;

message CreateUser {
  string name = 1;
}



[package]
name = "user"
version = "0.1.0"
edition = "2018"

[lib]

[[bin]] 
name = "server"
path = "src/server.rs"

[[bin]]
name = "client"
path = "src/client.rs"

[dependencies]
tonic = "0.5"
prost = "0.8"
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }


[build-dependencies]
tonic-build = "0.5"

My lib.rs file:

pub mod user {
    tonic::include_proto!("user");
}

pub mod server;
pub mod client{
    tonic::include_proto!("user");
}

main.rs:

use user::server;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    tracing::info!(message = "Started user server");
    server::start_server().await?;

    Ok(())
}

client.rs:

use user::{UserRequest };
use user::client::{UserClient}; // what should this import be, where does the client et created?

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut client = UserClient::connect("http://[::1]:50051").await?; // how do I create UserClient

    let request = tonic::Request::new(UserRequest {
        id: "1".into(),
    });

    println!("Sending request to gRPC Server...");
    let response = client.create_user(request).await?;

    println!("RESPONSE={:?}", response);

    Ok(())
}

For reference, I’m following:

https://tjtelan.com/blog/lets-build-a-single-binary-grpc-server-client-with-rust-in-2020/

https://blog.logrocket.com/rust-and-grpc-a-complete-guide/

https://dev.to/anshulgoyal15/a-beginners-guide-to-grpc-with-rust-3c7o

AMP_035
  • 167
  • 1
  • 2
  • 13
  • 1
    Here is another tonic tutorial [post](https://www.swiftdiaries.com/rust/tonic/). As per it, you need to compile the protobuf file first to generate the server and client stubs, then import the client stub (or server stub) into your client (or server) main source file. Hope this is helpful. – Joe_Jingyu Oct 25 '21 at 07:02
  • 1
    Per @Joe_Jingyu see the section in the tutorial referenced on using `build.rs` and `tonic_build` to compile the proto (instead of having to use e.g. `protoc`) as part of the `cargo build` step. This is a helpful feature of the `tonic*` crates. – DazWilkin Oct 25 '21 at 16:13
  • @Joe_Jingyu Thank you! This is helpful, that post is great, I appreciate it! – AMP_035 Oct 25 '21 at 22:50
  • @DazWilkin Thank you, this makes sense! I appreciate your help! – AMP_035 Oct 25 '21 at 22:50

0 Answers0