gRPC Web

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.

tonic-web = "0"

Then change your grpc_service and hybrid_make_service in to the following.

let grpc_service = tonic::transport::Server::builder()
        api_service::FortunesService { pool },

let hybrid_make_service = hybrid::hybrid(axum_make_service, grpc_service);


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 app folder.

npm install @protobuf-ts/plugin
npm install @protobuf-ts/grpcweb-transport

Update the scripts section to your app/package.json i.e.

"scripts": {
    "start": "parcel watch ./asset-pipeline/index.ts",
    "release": "parcel build ./asset-pipeline/index.ts",
    "protoc": "npx protoc --ts_out ./asset-pipeline --proto_path ../protos ../protos/api.proto"

Now the grpc client will be built and you can import it into your front end code.

import { GrpcWebFetchTransport } from "@protobuf-ts/grpcweb-transport";

For more read the docs on