पाठ 20 / 25

Browsers, gRPC-Web, Connect and Gateways

Reaching clients that cannot speak gRPC.

Bridging the gap

Browsers cannot control HTTP/2 framing and trailers directly, so they cannot call native gRPC. gRPC-Web is a variant protocol that browsers can use, usually through a proxy such as Envoy. The Connect protocol (from Buf) works over HTTP/1.1 and HTTP/2 with JSON or binary and is compatible with gRPC servers that support it. grpc-gateway and Envoy's transcoding generate REST/JSON endpoints from proto annotations, so external clients use plain HTTP.

REST mapping with HTTP annotations

google.api.http options used by grpc-gateway and transcoding proxies.

import "google/api/annotations.proto";

service OrderService {
  rpc GetOrder(GetOrderRequest) returns (Order) {
    option (google.api.http) = { get: "/v1/orders/{id}" };
  }
  rpc CreateOrder(CreateOrderRequest) returns (Order) {
    option (google.api.http) = { post: "/v1/orders" body: "order" };
  }
}

Keep one source of truth

Generating REST endpoints from the same proto avoids maintaining two diverging APIs.

त्वरित जाँच: Why can browsers not call native gRPC directly?

  • Browsers do not support HTTPS
  • Browser APIs do not give enough control over HTTP/2 framing and trailers
  • Protobuf is illegal in browsers
  • gRPC requires UDP
Answer

Browser APIs do not give enough control over HTTP/2 framing and trailers — Use gRPC-Web, Connect or a gateway.