Là một dự án API, grpc/grpc-web đã đạt 9.3k sao trên GitHub, ngôn ngữ JavaScript. gRPC for Web Clients
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
A JavaScript implementation of gRPC for browser clients. For more information,
including a quick start, see the gRPC-web documentation.
gRPC-web clients connect to gRPC services via a special proxy; by default,
gRPC-web uses Envoy.
In the future, we expect gRPC-web to be supported in language-specific web
frameworks for languages such as Python, Java, and Node. For details, see the
roadmap.
Streaming Support
gRPC-web currently supports 2 RPC modes:
Unary RPCs (example)
Server-side Streaming RPCs (example) (NOTE: Only when grpcwebtext mode is used.)
Client-side and Bi-directional streaming is not currently supported (see streaming roadmap).
Quick Start
Eager to get started? Try the Hello World example. From this example, you'll
learn how to do the following:
Define your service using protocol buffers
Implement a simple gRPC Service using Node.js
Configure the Envoy proxy
Generate protobuf message classes and client service stub for the client
Compile all the JS dependencies into a static library that can be consumed
by the browser easily
Advanced Demo: Browser Echo App
You can also try to run a more advanced Echo app from the browser with a
streaming example.
You can then use Browserify, Webpack, Closure Compiler, etc. to resolve imports
at compile time.
Import Style
import_style=closure: The default generated code has
Closure goog.require()
import style.
import_style=commonjs: The
CommonJS style require() is
also supported.
import_style=commonjs+dts: In addition to above, a .d.ts
typings file will also be generated for the protobuf messages and service stub
import_style=typescript: The service stub will be generated
in TypeScript. See TypeScript Support below for information on how to
generate TypeScript files.
Note: The commonjs+dts and typescript styles are only supported by
--grpc-web_out=import_style=..., not by --js_out=import_style=....
Wire Format Mode
For more information about the gRPC-web wire format, see the
specification.
mode=grpcwebtext: The default generated code sends the payloads in the
grpc-web-text format.
Content-Type: application/grpc-web-text
Payloads are base64-encoded.
Both unary and server streaming calls are supported.
mode=grpcweb: A binary protobuf format is also supported.
Content-Type: application/grpc-web+proto
Payloads are in the binary protobuf format.
Only unary calls are supported.
How It Works
Let's take a look at how gRPC-web works with a simple example. You can find out
how to build, run and explore the example yourself in
Build and Run the Echo Example.
1. Define your service
The first step when creating any gRPC service is to define it. Like all gRPC
services, gRPC-web uses
protocol buffers to define
its RPC service methods and their message request and response types.
Next you need to have a gRPC server that implements the service interface and a
gateway proxy that allows the client to connect to the server. Our example
builds a simple Node gRPC backend server and the Envoy proxy.
For the Echo service: see the
service implementation.
For the Envoy proxy: see the
config YAML file.
3. Write your JS client
Once the server and gateway are up and running, you can start making gRPC calls
from the browser!
Create your client:
var echoService = new proto.mypackage.EchoServiceClient(
'http://localhost:8080');
Make a unary RPC call:
var request = new proto.mypackage.EchoRequest();
request.setMessage(msg);
var metadata = {'custom-header-1': 'value1'};
echoService.echo(request, metadata, function(err, response) {
if (err) {
console.log(err.code);
console.log(err.message);
} else {
console.log(response.getMessage());
}
});
Server-side streaming:
var stream = echoService.serverStreamingEcho(streamRequest, metadata);
stream.on('data', function(response) {
console.log(response.getMessage());
});
stream.on('status', function(status) {
console.log(status.code);
console.log(status.details);
console.log(status.metadata);
});
stream.on('end', function(end) {
// stream end signal
});
// to close the stream
stream.cancel();
For an in-depth tutorial, see this
page.
Setting a Deadline
You can set a deadline for your RPC by setting a deadline header. The value
should be a Unix timestamp, in milliseconds.
var deadline = new Date();
deadline.setSeconds(deadline.getSeconds() + 1);
client.sayHelloAfterDelay(request, {deadline: deadline.getTime().toString()},
(err, response) => {
// err will be populated if the RPC exceeds the deadline
...
});
TypeScript Support
The grpc-web runtime library is now fully written in TypeScript! This provides first-class type safety, zero runtime dependencies, and built-in type definitions out of the box.
You can simply import it into your TypeScript projects without needing external typings.
When using the protoc-gen-grpc-web protoc plugin, mentioned above, pass in
either:
Do not use import_style=typescript for --js_out, it will silently be
ignored. Instead you should use --js_out=import_style=commonjs, or
--js_out=import_style=commonjs,binary if you are using mode=grpcweb. The
--js_out plugin will generate JavaScript code (echo_pb.js), and the
-grpc-web_out plugin will generate a TypeScript definition file for it
(echo_pb.d.ts). This is a temporary hack until the --js_out supports
TypeScript itself.
For example, this is the command you should use to generate TypeScript code
using the binary wire format:
grpc/grpc-web thuộc nhóm API trên TopGit, cùng 3 topic GitHub. Trang Trending và Topics liệt kê các repo cùng số sao và cùng ngôn ngữ để so sánh.
Đọc thêm về grpc/grpc-web ở đâu?
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/grpc/grpc-web là nguồn chính thức.
grpc/grpc-web có phải mã nguồn mở không?
Có — grpc/grpc-web phát hành theo license Apache-2.0, nghĩa là mã nguồn mở để đọc, fork và (tùy license) tái sử dụng. Mã: github.com/grpc/grpc-web.
grpc/grpc-web có trang demo không?
Dự án có trang chủ ở https://grpc.io. Tab "Readme" ở trang này thường có ảnh chụp và hướng dẫn bắt đầu nhanh.
grpc/grpc-web dùng license gì?
grpc/grpc-web phát hành theo license Apache-2.0. Nên mở file LICENSE trên GitHub để xác nhận — license metadata đôi khi lệch với thực tế dự án.
grpc/grpc-web là gì?
grpc/grpc-web (grpc/grpc-web) là dự án JavaScript trên GitHub. Theo mô tả gốc: gRPC for Web Clients
Vì sao grpc/grpc-web được xếp vào nhóm API?
TopGit xếp grpc/grpc-web vào nhóm API dựa trên GitHub topics và mô tả của repo (gắn thẻ: "grpc", "javascript", "web"). Việc phân loại dựa trên metadata thật của repo, không phải đoán theo cảm tính biên tập.
Đọc đầy đủ README ở tab phía trên.
Muốn nghe thêm một ý kiến về grpc-web?
Hỏi một AI đọc được trang này — một cú bấm là có ngay nhận định về grpc-web.