Browser to Server communications
To be able to use gRPC from the browser we need to enable gRPC Web.
Tonic comes with a gRPC-Web to gRPC layer so let's add that.
Install Tonic Web
Add the following to your Cargo.toml below the dependencies section.
Then change your
main.rs to the following.
let grpc_service = builder .accept_http1 .add_service .into_service; let hybrid_make_service = hybrid;
Now the server will respond to gRPC and gRPC web calls. We can test this with Bloom RPC by clicking on the GRPC button and setting it to WEB.
Generating Typescript Client Stubs
To generate gRPC client stubs in your asset pipeline we can use the protobuf-ts project. Run the following from the
scripts section to your
... "scripts": , ...
Now the grpc client will be built and you can import it into your front end code.
For more read the docs on https://github.com/timostamm/protobuf-ts