gRPC-Web Client Runtime Library

2.0.2 · active · verified Tue Apr 21

grpc-web is the JavaScript client runtime library that enables browser-based applications to communicate with gRPC services. It functions by connecting to gRPC services through a specialized gateway proxy, such as Envoy, which has built-in gRPC-Web support. The current stable version is 2.0.2, with recent releases addressing bug fixes and minor improvements, including TypeScript compatibility updates. The project maintains a steady release cadence for bug fixes and incremental features. Key differentiators include its focus on browser environments, robust TypeScript support for generated client stubs, and the reliance on `protoc` and `protoc-gen-grpc-web` for generating client code and message definitions from `.proto` files, providing a full-stack gRPC experience for web clients.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate a gRPC-Web client, create a request, send it to a gRPC service through a proxy, and handle the response and potential errors using TypeScript.

import * as grpcWeb from 'grpc-web';
import { EchoServiceClient } from './generated/echo_grpc_web_pb';
import { EchoRequest, EchoResponse } from './generated/echo_pb';

// Ensure you have a gRPC-Web proxy (e.g., Envoy) running at this address
const GRPC_PROXY_ADDRESS = 'http://localhost:8080';

// Create a client instance for your gRPC service
const echoService = new EchoServiceClient(GRPC_PROXY_ADDRESS, null, null);

// Create a new request message
const request = new EchoRequest();
request.setMessage('Hello from gRPC-Web!');

// Define custom metadata (optional)
const metadata = { 'custom-header-1': 'value1' };

// Make the gRPC call
const call = echoService.echo(
  request,
  metadata,
  (err: grpcWeb.RpcError, response: EchoResponse) => {
    if (err) {
      console.error('Error during gRPC call:', err.code, err.message);
      return;
    }
    console.log('Received message:', response.getMessage());
  }
);

// Handle status updates (optional, for streaming or status monitoring)
call.on('status', (status: grpcWeb.Status) => {
  if (status.code !== grpcWeb.StatusCode.OK) {
    console.warn('gRPC call status:', status.code, status.details);
  } else {
    console.info('gRPC call completed successfully.');
  }
});

// Handle stream errors (for server streaming, optional)
call.on('error', (err: grpcWeb.RpcError) => {
  console.error('Stream error:', err.message);
});

view raw JSON →