百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
G

grpc-web

> 编程语言
开源

用于 Golang 和 TypeScript 的 gRPC Web 实现

4.5K stars0 点赞0 次浏览
访问官网GitHub

工具介绍

用于 Golang 和 TypeScript 的 gRPC Web 实现

gRPC-Web: Typed Frontend Development

Please note that this repo is in maintenance mode, and we recommend users migrate to the official grpc-web client: https://github.com/grpc/grpc-web.

gRPC is a modern, HTTP2-based protocol, that provides RPC semantics using the strongly-typed binary data format of protocol buffers across multiple languages (C++, C#, Golang, Java, Python, NodeJS, ObjectiveC, etc.)

gRPC-Web is a cutting-edge spec that enables invoking gRPC services from modern browsers.

If you are looking for gRPC support for Node.js there is an official Node.js gRPC library. This package supports Node.js, but requires that the server has the gRPC-Web compatibility layer (read on to understand more).

Components of the stack are based on Golang and TypeScript:

  • grpcweb - a Go package that wraps an existing grpc.Server as a gRPC-Web http.Handler for both HTTP2 and HTTP/1.1.
  • grpcwebproxy - a Go-based stand-alone reverse proxy for classic gRPC servers (e.g. in Java or C++) that exposes their services over gRPC-Web to modern browsers.
  • ts-protoc-gen - a TypeScript plugin for the protocol buffers compiler that provides strongly typed message classes and method definitions.
  • @improbable-eng/grpc-web - a TypeScript gRPC-Web client library for browsers (and Node.js).

Why?

With gRPC-Web, it is extremely easy to build well-defined, easy to reason about APIs between browser frontend code and microservices. Frontend development changes significantly:

  • no more hunting down API documentation - .proto is the canonical format for API contracts.
  • no more hand-crafted JSON call objects - all requests and responses are strongly typed and code-generated, with hints available in the IDE.
  • no more dealing with methods, headers, body and low level networking - everything is handled by grpc.invoke.
  • no more second-guessing the meaning of error codes - gRPC status codes are a canonical way of representing issues in APIs.
  • no more one-off server-side request handlers to avoid concurrent connections - gRPC-Web is based on HTTP2, with multiplexes multiple streams over the same connection.
  • no more problems streaming data from a server - gRPC-Web supports both 1:1 RPCs and 1:many streaming requests.
  • no more data parse errors when rolling out new binaries - backwards and forwards-compatibility of requests and responses.

In short, gRPC-Web moves the interaction between frontend code and microservices from the sphere of hand-crafted HTTP requests to well-defined user-logic methods.

Client-side (grpc-web) Docs

Note: You'll need to add gRPC-Web compatibility to your server through either grpcweb or grpcwebproxy.

API Docs for grpc-web client can be found here

Example

For a self-contained demo of a Golang gRPC service called from a TypeScript project, see example. It contains most of the initialization code that performs the magic. Here's the application code extracted from the example:

You use .proto files to define your service. In this example, one normal RPC (GetBook) and one server-streaming RPC (QueryBooks):

syntax = "proto3";

message Book {
  int64 isbn = 1;
  string title = 2;
  string author = 3;
}

message GetBookRequest {
  int64 isbn = 1;
}

message QueryBooksRequest {
  string author_prefix = 1;
}

service BookService {
  rpc GetBook(GetBookRequest) returns (Book) {}
  rpc QueryBooks(QueryBooksRequest) returns (stream Book) {}
}

And implement it in Go (or any other gRPC-supported language):

…

You will be able to access it in a browser using TypeScript (and equally JavaScript after transpiling):

…

Usage with React

  • Example project using gRPC-Web with React and Go

Browser Support

The @improbable-eng/grpc-web client uses multiple techniques to efficiently invoke gRPC services. Most modern browsers support the Fetch API, which allows for efficient reading of partial, binary responses. For older browsers, it automatically falls back to XMLHttpRequest.

The gRPC semantics encourage you to make multiple requests at once. With most modern browsers supporting HTTP2, these can be executed over a single TLS connection. For older browsers, gRPC-Web falls back to HTTP/1.1 chunk responses.

This library is tested against:

  • Chrome >= 41
  • Firefox >= 38
  • Edge >= 13
  • IE >= 11
  • Safari >= 8

Node.js Support

The @improbable-eng/grpc-web client also supports Node.js through a transport that uses the http and https packages. Usage does not vary from browser usage as transport is determined at runtime.

If you want to use the @improbable-eng/grpc-web client in a node.js environment with Typescript, you must include dom in the "lib" Array in your tsconfig.json otherwise tsc will be unable to find some type declarations to compile. Note that dom will be included automatically if you do not declare lib in your configration and your target is one of es5 or es6. (See Typescript compiler options).

{
  "compilerOptions": {
    "lib": [ "dom", /* ... */ ],
  }
}

Please note - There is an official Node.js gRPC library that does not require the server to support gRPC-Web

Client-side streaming

It is very important to note that the gRPC-Web spec currently does not support client-side streaming. This is unlikely to change until new whatwg fetch/streams API lands in browsers. As such, if you plan on using gRPC-Web you're limited to:

  • unary RPCs (1 request 1 response)
  • server-side streaming RPCs (1 request N responses)

This, however, is useful for a lot of frontend functionality.

Note that @improbable-eng/grpc-web provides a built-in websocket transport that can support client-side/bi-directional streaming RPCs.

Status

The code here is alpha quality. It is being used for a subset of Improbable's frontend single-page apps in production.

Known Limitations

See the @improbable-eng/grpc-web client Transport Documentation for a list of Web Browser caveats.

Contributing

See CONTRIBUTING

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScriptbrowsergolanggrpcgrpc-web

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言