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.
Add the following to your Cargo.toml below the dependencies section.
= "0"
Then change your grpc_service
and hybrid_make_service
in 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.
To generate gRPC client stubs in your asset pipeline we can use the protobuf-ts project. Run the following from the app
folder.
Update the scripts
section to your app/package.json
i.e.
...
"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