Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any interest in a PR to support WASM based clients? #331

Closed
john-hern opened this issue Nov 5, 2020 · 5 comments
Closed

Any interest in a PR to support WASM based clients? #331

john-hern opened this issue Nov 5, 2020 · 5 comments

Comments

@john-hern
Copy link

john-hern commented Nov 5, 2020

Hey Maintainers,

I have created a fork of tarpc, that supports WASM clients. I'm curious what the apatite is for supporting such a PR, maybe with an experimental feature flag? I have a few forked repos that have it all working end to end, see the links below. The nice thing is that the changes aren't heavy for tarpc. It really is just a few shims for the std::time/tokio::time calls and enabling a dependency feature flag (WASM support for rand crate). The only issue is I'm not sure if the time shim library is being actively maintained, I plan to file an issue with that project and see if they would accept a PR. If they aren't actively maintaining the project I can fork it and maintain it since shims aren't a lot of work and I'd like to explore this approach in a project I'm working on.

Side note: I started this a couple of weeks back, so my code is targeting tarpc 0.22, however I did test it with 0.23 (See the other branch) and it appears to work fine (IE, it compiles). I couldn't test it since my client/server have a dependency that hasn't moved to tokio 0.3 yet.

tarpc fork:
master...john-hern:wasm_support
Notes: I'll likely move the SystemTimeExt trait/impl into the wasm-timer fork/PR and implement from/into on the SystemTime type.

wasm-timer fork:
tomaka/wasm-timer@master...john-hern:tokioShim
Notes: Nothing interesting here except the reimplementation of tokio's timeout API using the shims.

server/client example.
https://github.com/john-hern/tarpc-wasm-client-tcp-server-example
Notes:
The server is using tokio + ws_stream_tungstenite to create the byte stream/sink over a websocket.
The client is a yew application, using ws_stream_wasm wasm_bindgen (spawn_local).

There are probably some caveats with edge cases. For example, the time shims wont work in a web worker. There is an issue in web-sys to address this. Once it's fixed, it should be possible to use this code in a web worker. Also, since wasms are currently single threaded there are probably some perf concerns. I'm pretty sure other things may pop up.

Thoughts?
*edit: Had the wrong link for the wasm-timer fork.

@tikue
Copy link
Collaborator

tikue commented Nov 7, 2020

Hey, this is very cool! I didn't know futures worked on WASM :) I am happy to merge WASM support under a feature flag, especially since it appears to require such minimal changes. I might suggest providing a tarpc::time module facade so that the other modules don't have to have #[cfg(wasm)] scattered about.

@john-hern
Copy link
Author

Awesome. I'll reach out to the wasm-timer team and see if I can elicit a response. I already updated the branches to add the façade (Good suggestion), and moved over the SystemTime conversation to an into implementation in that crate. I'll circle back in a week or two once I get a better feeling about their ability to support the project long term. I'll update this thread with a link to the issue once I create it for tracking.

@bbarker
Copy link

bbarker commented Jan 25, 2021

Any updates on this? I'd love to be able to have RPC clients in the browser!

@tikue
Copy link
Collaborator

tikue commented Jun 19, 2021

@john-hern closing due to inactivity but feel free to reopen if you pick this back up!

@tikue tikue closed this as completed Jun 19, 2021
@davidatsurge
Copy link

FYI, the following patch (to 75b15fe) allows tarpc to compile on wasm32-unknown-unknown now.

diff --git a/Cargo.toml b/Cargo.toml
index c7557d0..3e8109d 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -2,7 +2,7 @@
 resolver = "2"
 
 members = [
-    "example-service",
+    # "example-service",
     "tarpc",
     "plugins",
 ]
diff --git a/tarpc/Cargo.toml b/tarpc/Cargo.toml
index b2f037b..34255b3 100644
--- a/tarpc/Cargo.toml
+++ b/tarpc/Cargo.toml
@@ -61,6 +61,8 @@ tracing = { version = "0.1", default-features = false, features = [
 tracing-opentelemetry = { version = "0.17.2", default-features = false }
 opentelemetry = { version = "0.17.0", default-features = false }
 
+[target.wasm32-unknown-unknown.dependencies]
+getrandom = { version = "0.2.7" , features = [ "js" ] }
 
 [dev-dependencies]
 assert_matches = "1.4"

Note that we're not compiling example-service/ at all since I didn't look into making that wasm-compatible. Also, I haven't actually tried to see if it not only compiles, but it works (since this is Rust, I assume that to be the case).

I have put aside tarpc for now, but I thought I'd share this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants