--- url: "https://grpc.io/docs/platforms/web/basics/" titulek: "Basics tutorial | Web | gRPC" h1: "Basics tutorial" popis: "A basic tutorial introduction to gRPC-web." jazyk: "en" kanonicka: "https://grpc.io/docs/platforms/web/basics/" drobecky: ["Docs", "Platforms", "Web", "Basics tutorial"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-13 21:00:06" znaku: 7455 slov: 809 kodovani: "utf-8" sha256: "8ccbc1d29838ea85de21e5104c616e0320620ee474892c7b627ef19a9431ee6e" --- # Basics tutorial A basic tutorial introduction to gRPC-web. Contents # Basics tutorial A basic tutorial introduction to gRPC-web. This tutorial provides a basic introduction on how to use gRPC-Web from browsers. By walking through this example you’ll learn how to: - Define a service in a .proto file. - Generate client code using the protocol buffer compiler. - Use the gRPC-Web API to write a simple client for your service. It assumes a passing familiarity with [protocol buffers](https://protobuf.dev/overview). ### Why use gRPC and gRPC-Web? With gRPC you can define your service once in a .proto file and implement clients and servers in any of gRPC’s supported languages, which in turn can be run in environments ranging from servers inside a large data center to your own tablet - all the complexity of communication between different languages and environments is handled for you by gRPC. You also get all the advantages of working with protocol buffers, including efficient serialization, a simple IDL, and easy interface updating. gRPC-Web lets you access gRPC services built in this manner from browsers using an idiomatic API. ### Define the Service The first step when creating a gRPC service is to define the service methods and their request and response message types using protocol buffers. In this example, we define our `EchoService` in a file called [`echo.proto`](https://github.com/grpc/grpc-web/blob/0.4.0/net/grpc/gateway/examples/echo/echo.proto). For more information about protocol buffers and proto3 syntax, please see the [protobuf documentation](https://protobuf.dev/). ``` message EchoRequest { string message = 1; } message EchoResponse { string message = 1; } service EchoService { rpc Echo(EchoRequest) returns (EchoResponse); } ``` ### Implement gRPC Backend Server Next, we implement our EchoService interface using Node in the backend gRPC `EchoServer`. This will handle requests from clients. See the file [`node-server/server.js`](https://github.com/grpc/grpc-web/blob/master/net/grpc/gateway/examples/echo/node-server/server.js) for details. You can implement the server in any language supported by gRPC. Please see the [main page](/docs/) for more details. ``` function doEcho(call, callback) { callback(null, {message: call.request.message}); } ``` ### Configure the Envoy Proxy In this example, we will use the [Envoy](https://www.envoyproxy.io/) proxy to forward the gRPC browser request to the backend server. You can see the complete config file in [envoy.yaml](https://github.com/grpc/grpc-web/blob/master/net/grpc/gateway/examples/echo/envoy.yaml) To forward the gRPC requests to the backend server, we need a block like this: ``` admin: address: socket_address: { address: 0.0.0.0, port_value: 9901 } static_resources: listeners: - name: listener_0 address: socket_address: { address: 0.0.0.0, port_value: 8080 } filter_chains: - filters: - name: envoy.http_connection_manager typed_config: "@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager codec_type: auto stat_prefix: ingress_http route_config: name: local_route virtual_hosts: - name: local_service domains: ["*"] routes: - match: { prefix: "/" } route: { cluster: echo_service } http_filters: - name: envoy.grpc_web typed_config: "@type": type.googleapis.com/envoy.extensions.filters.http.grpc_web.v3.GrpcWeb - name: envoy.filters.http.router typed_config: "@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router clusters: - name: echo_service connect_timeout: 0.25s type: LOGICAL_DNS typed_extension_protocol_options: envoy.extensions.upstreams.http.v3.HttpProtocolOptions: "@type": type.googleapis.com/envoy.extensions.upstreams.http.v3.HttpProtocolOptions explicit_http_config: http2_protocol_options: {} lb_policy: ROUND_ROBIN load_assignment: cluster_name: echo_service endpoints: - lb_endpoints: - endpoint: address: socket_address: address: node-server port_value: 9090 ``` You may also need to add some CORS setup to make sure the browser can request cross-origin content. In this simple example, the browser makes gRPC requests to port `:8080`. Envoy forwards the request to the backend gRPC server listening on port `:9090`. ### Generate Protobuf Messages and Service Client Stub To generate the protobuf message classes from our `echo.proto`, run the following command: ``` protoc -I=$DIR echo.proto \ --js_out=import_style=commonjs:$OUT_DIR ``` The `import_style` option passed to the `--js_out` flag makes sure the generated files will have CommonJS style `require()` statements. To generate the gRPC-Web service client stub, first you need the gRPC-Web protoc plugin. To compile the plugin `protoc-gen-grpc-web`, you need to run this from the repo’s root directory: ``` cd grpc-web sudo make install-plugin ``` To generate the service client stub file, run this command: ``` protoc -I=$DIR echo.proto \ --grpc-web_out=import_style=commonjs,mode=grpcwebtext:$OUT_DIR ``` In the `--grpc-web_out` param above: - `mode` can be `grpcwebtext` (default) or `grpcweb` - `import_style` can be `closure` (default) or `commonjs` Our command generates the client stub, by default, to the file `echo_grpc_web_pb.js`. ### Write JS Client Code Now you are ready to write some JS client code. Put this in a `client.js` file. ``` const {EchoRequest, EchoResponse} = require('./echo_pb.js'); const {EchoServiceClient} = require('./echo_grpc_web_pb.js'); var echoService = new EchoServiceClient('http://localhost:8080'); var request = new EchoRequest(); request.setMessage('Hello World!'); echoService.echo(request, {}, function(err, response) { // ... }); ``` You will need a `package.json` file ``` { "name": "grpc-web-commonjs-example", "dependencies": { "google-protobuf": "^3.6.1", "grpc-web": "^0.4.0" }, "devDependencies": { "browserify": "^16.2.2", "webpack": "^4.16.5", "webpack-cli": "^3.1.0" } } ``` ### Compile the JS Library Finally, putting all these together, we can compile all the relevant JS files into one single JS library that can be used in the browser. ``` npm install npx webpack client.js ``` Now embed `dist/main.js` into your project and see it in action! Last modified November 25, 2024: [feat: move the $ shell line indicator to scss (#1354) (ab8b3af)](https://github.com/grpc/grpc.io/commit/ab8b3af6ddfccb1995e1a06367593b14972aaf4b) [View page source](https://github.com/grpc/grpc.io/tree/main/content/en/docs/platforms/web/basics.md) [Edit this page](https://github.com/grpc/grpc.io/edit/main/content/en/docs/platforms/web/basics.md) [Create child page](https://github.com/grpc/grpc.io/new/main/content/en/docs/platforms/web/basics.md?filename=change-me.md&value=---%0Atitle%3A+%22Long+Page+Title%22%0AlinkTitle%3A+%22Short+Nav+Title%22%0Aweight%3A+100%0Adescription%3A+%3E-%0A+++++Page+description+for+heading+and+indexes.%0A---%0A%0A%23%23+Heading%0A%0AEdit+this+template+to+create+your+new+page.%0A%0A%2A+Give+it+a+good+name%2C+ending+in+%60.md%60+-+e.g.+%60getting-started.md%60%0A%2A+Edit+the+%22front+matter%22+section+at+the+top+of+the+page+%28weight+controls+how+its+ordered+amongst+other+pages+in+the+same+directory%3B+lowest+number+first%29.%0A%2A+Add+a+good+commit+message+at+the+bottom+of+the+page+%28%3C80+characters%3B+use+the+extended+description+field+for+more+detail%29.%0A%2A+Create+a+new+branch+so+you+can+preview+your+new+file+and+request+a+review+via+Pull+Request.%0A) [Create documentation issue](https://github.com/grpc/grpc.io/issues/new?title=Basics%20tutorial) [Create project issue](https://github.com/grpc/grpc.io/issues/new)