gRPC for Web Clients
Go to file
Stanley Cheung 0cca437b65 Update doc as well 2018-03-14 11:26:02 -07:00
javascript/net/grpc/web Set X-User-Agent header according to spec 2018-03-01 13:23:47 -08:00
net/grpc/gateway Update doc as well 2018-03-14 11:26:02 -07:00
scripts gRPC Sync / Import 2018-03-07 12:24:55 -08:00
third_party gRPC Sync / Import 2018-03-07 12:24:55 -08:00
.gitmodules Remove the protobuf submodule, use the protobuf provided protobuf instead. 2016-11-18 18:48:31 -08:00
.travis.yml Fix .travis.yml 2018-02-12 14:47:38 -08:00
AUTHORS Update LICENSE file and Add AUTHORS file 2018-02-22 11:37:25 -08:00
CONTRIBUTING.md Initial commit 2016-11-02 15:10:45 -07:00
INSTALL.md Update docs 2018-02-22 15:32:00 -08:00
LICENSE Update LICENSE file and Add AUTHORS file 2018-02-22 11:37:25 -08:00
Makefile Decouple example build from nginx build 2018-01-19 12:36:19 -08:00
PATENTS Initial commit 2016-11-02 15:10:45 -07:00
PROTOCOL-WEB.md Update PROTOCOL-WEB.md 2018-02-28 17:19:35 -08:00
README.md Update docs 2018-02-22 15:32:00 -08:00
ROADMAP.md Update ROADMAP.md 2018-03-02 16:05:22 +08: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, mainly for early adopters to provide feedback on the JS API (both gRPC and Protobuf). 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 Nginx. However, Nginx doesn't support HTTP/2 to backends as of Q3/2017, and therefore the gateway can't be used as a reverse proxy for load balancing - if you need to use a reverse proxy as well you'll need to deploy a second gateway. We have also added gRPC-Web support to Envoy, if you wish to use this instead of an Nginx gateway. In future, we expect gRPC-Web to be supported in language-specific Web frameworks, such as Go, Java, and Node, which will eliminate the need to deploy a gateway at all.

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 Nginx gateway. 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:9091');

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());
});