tonic icon indicating copy to clipboard operation
tonic copied to clipboard

Add client feature flag to support connect_with_connector in wasm32 targets

Open lucasmerlin opened this issue 1 year ago • 4 comments

This is an updated version of @boxdot's PR #693. The main purpose is to allow compiling tonic for wasm32-unknown-unknown with the connect_with_connector function, so you could use custom connectors in wasm.

Motivation

The main motivation is to allow using something like https://github.com/boxdot/tonic-ws-transport to use tonic in browsers over websockets. grpc-web has a limit on concurrent conections that are open and doesn't allow for client streaming. There is also a related issue for this, #491.

Solution

The connect_with_connector function, as well as the Endpoint and Channel structs, are gated behind the transport feature. Enabling this feature also enables things that are impossible to compile for wasm. This PR introduces a client feature that that enables everything necessary to make connect_with_connector work in wasm and disables anything that fails to compile for wasm.

An alternative to adding a new client feature could also be to gate everything that doesn't compile for wasm behind a #[cfg()] attribute that is disabled for wasm.

I could also add a test that checks whether compilation to wasm works with the client feature, so this won't break in the future.

lucasmerlin avatar Jan 09 '24 22:01 lucasmerlin

@lucasmerlin I'm also interested in this. Any chance you can address the conflicts?

flokli avatar Jun 14 '24 04:06 flokli

Second this, would love to see it come to fruition.

pbower avatar Jun 14 '24 05:06 pbower

Hi, I gave it a shot and got the native version of tonic-ws-transport working natively, but I'm struggling to get tonic to compile for wasm again. hyper-utils e.g. depends on tower/make which in turn depends on tokio features which aren't available on wasm. So I think there will need to be some patches to other crates to get this working again, which I unfortunately don't have the time for right now.

If someone else wants to give it a try, these are the branches with my rebased changes: https://github.com/lucasmerlin/tonic/tree/wasm-client-feature-hyper-1.0 https://github.com/lucasmerlin/tonic-ws-transport/tree/hyper-1.0

That said, I'm still using tonic-ws-transport with tonic 0.10.x in my project and it's working really well for me.

lucasmerlin avatar Jun 18 '24 00:06 lucasmerlin

IIRC there's some work ongoing in hyper-utils to make some dependencies optional, though tower still seems to be required after https://github.com/hyperium/hyper-util/pull/105. cc @tottoto for visibility.

flokli avatar Jun 18 '24 04:06 flokli