gRPC for Web Clients
Go to file
Stanley Cheung a9aa7dffb0 Added multiple proxies interoperability 2018-08-03 23:49:02 -07:00
etc Added multiple proxies interoperability 2018-08-03 23:49:02 -07:00
javascript/net/grpc/web Added multiple proxies interoperability 2018-08-03 23:49:02 -07:00
kokoro Add more tests to be run by kokoro webhook 2018-08-02 21:17:01 -07:00
net/grpc/gateway Added multiple proxies interoperability 2018-08-03 23:49:02 -07:00
packages/grpc-web Added multiple proxies interoperability 2018-08-03 23:49:02 -07:00
scripts Add more tests to be run by kokoro webhook 2018-08-02 21:17:01 -07:00
third_party gRPC Sync 2018-07-11 17:34:41 -07:00
.gitignore Add bazel integration and tests 2018-07-26 19:35:56 -07:00
.gitmodules Remove the protobuf submodule, use the protobuf provided protobuf instead. 2016-11-18 18:48:31 -08:00
AUTHORS Update LICENSE file and Add AUTHORS file 2018-02-22 11:37:25 -08:00
BROWSER-FEATURES.md Clarify the need for HTTP status mapping 2018-07-12 15:58:21 -07:00
CONTRIBUTING.md Initial commit 2016-11-02 15:10:45 -07:00
INSTALL.md Replace default proxy with Envoy to run end-to-end Echo example; 2018-07-02 23:28:43 -07:00
LICENSE Update LICENSE file and Add AUTHORS file 2018-02-22 11:37:25 -08:00
Makefile Added multiple proxies interoperability 2018-08-03 23:49:02 -07:00
PATENTS Initial commit 2016-11-02 15:10:45 -07:00
README.md Added multiple proxies interoperability 2018-08-03 23:49:02 -07:00
ROADMAP.md Fix typo in roadmap 2018-07-09 21:16:46 -07:00
WORKSPACE Add bazel integration and tests 2018-07-26 19:35:56 -07:00
docker-compose.yml Added multiple proxies interoperability 2018-08-03 23:49:02 -07:00

README.md

Overview

gRPC-Web provides a Javascript client library that lets browser clients access a gRPC server. You can find out much more about gRPC in its own website.

The current release is a Beta release, and we expect to announce General-Available by Oct. 2018.

The JS client library has been used for some time by Google and Alphabet projects with the Closure compiler and its TypeScript generator (which has not yet been open-sourced).

gRPC-Web clients connect to gRPC servers via a special gateway proxy: our provided version uses Envoy, in which gRPC-Web support is built-in. Envoy will become the default gateway for gRPC-Web by GA.

In the future, we expect gRPC-Web to be supported in language-specific Web frameworks, such as Python, Java, and Node. See the roadmap doc.

Quick start

Try gRPC-Web and run a quick Echo example from the browser!

From the repo root directory:

$ docker-compose up echo-server envoy commonjs-client-example

Open a browser tab, and inspect

http://localhost:8081/echo_commonjs_test.html

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.

service EchoService {
  rpc Echo(EchoRequest) returns (EchoResponse);

  rpc ServerStreamingEcho(ServerStreamingEchoRequest)
      returns (stream ServerStreamingEchoResponse);
}

2. Build the server

Next you need to have a gRPC server that implements the service interface and a gateway that allows the client to connect to the server. Our example builds a simple C++ gRPC backend server and the Envoy proxy. You can find out more in the Echo Example.

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.grpc.gateway.testing.EchoServiceClient(
  'http://localhost:8080');

Make a unary RPC call

var unaryRequest = new proto.grpc.gateway.testing.EchoRequest();
unaryRequest.setMessage(msg);
echoService.echo(unaryRequest, {},
  function(err, response) {
    console.log(response.getMessage());
  });

Server-side streaming is supported!

var stream = echoService.serverStreamingEcho(streamRequest, {});
stream.on('data', function(response) {
  console.log(response.getMessage());
});

Proxy interoperability

Multiple proxies supports the gRPC-Web protocol. Currently, the default proxy is Envoy.

$ docker-compose up echo-server envoy commonjs-client-example

An alternative is to build Nginx that comes with this repository.

$ docker-compose up echo-server nginx commonjs-client-example

Finally, you can also try this gRPC-Web Go Proxy.

$ docker-compose up echo-server grpcwebproxy binary-client-example