devashishdxt / tonic-web-wasm-client

Other
104 stars 28 forks source link

Question: Return Client from function to JS #63

Closed hg-gr closed 1 week ago

hg-gr commented 1 week ago

Thanks a bunch for working on something so cool, I am able to get things to compile which is great. I want to try the following:

use market_data::global_pricing_service_client::GlobalPricingServiceClient;
use tonic_web_wasm_client::Client;
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub async fn get_client() -> GlobalPricingServiceClient<Client> {
    let client = Client::new("http://localhost:50051".to_string());
    GlobalPricingServiceClient::new(client)
}

But my return type is not working, the error is that my GlobalPricingServiceClient type is not able to become a JS type.

Have you achieved something similar or are able to explain how I can do this?

Thanks again!

devashishdxt commented 1 week ago

Hi. If you're trying to use this crate in JS. I'd suggest to use some pre-existing JS libraries to call web gRPC. This crate is primarily intended for use in Rust code that gets compiled to web assembly. If you still want to go the Rust route, you should be definitely be able to do this. I don't have a working example, but you can read how to do this here: https://rustwasm.github.io/docs/wasm-bindgen/