grpc-dart Documentation

repository·master·Indexed 21 days ago

https://github.com/grpc/grpc-dart

Dart implementation of the gRPC framework, a high-performance RPC framework optimized for mobile and HTTP/2. Supports Dart native, Flutter, and Web (via grpc-web), as well as Unix Domain Sockets (UDS) for Dart SDK >= 2.8.0. Includes tools for regenerating gRPC stubs using protoc and the protoc_plugin, and provides specialized client channels like GrpcOrGrpcWebClientChannel for cross-platform compatibility.

Tokens
10K
Snippets
41
Records
49
Agent score
75%

What's inside grpc-dart

  1. Supported platforms for grpc-dart

    master

    The grpc-dart implementation supports the following platforms and environments:

    • Dart native
    • Flutter
    • Web: Supported via package:grpc/grpc_web.dart (requires grpc-web).
    • UDS (Unix Domain Sockets): Supported with Dart SDK version >= 2.8.0.
  2. Regenerate Dart gRPC stubs and message classes

    master

    The Dart gRPC stubs and message classes are generated from protobuf definitions. To regenerate them, you need to have the following prerequisites:

    Prerequisites

    Installation

    To install the Dart protoc plugin, run:

    pub global activate protoc_plugin

    Ensure ~/.pub-cache/bin is added to your PATH.

    Regeneration Steps

    1. Set the PROTOBUF environment variable to the path of your google/protobuf clone.
    2. Set the GOOGLEAPIS environment variable to the path of your googleapis/googleapis clone.
    3. Run the regeneration script:
    tool/regenerate.sh
    # Install plugin
    $ pub global activate protoc_plugin
    
    # Set environment variables and regenerate
    $ export PROTOBUF=/path/to/google/protobuf
    $ export GOOGLEAPIS=/path/to/googleapis/googleapis
    $ tool/regenerate.sh
  3. Run the grpc-web sample code

    master

    After setting up the server and prerequisites, follow these steps to run the Dart client in a web environment:

    1. Start the server: Use Docker Compose to start the required gRPC-Web infrastructure:
      docker-compose up node-server envoy commonjs-client
    2. Install Dart dependencies: Navigate to the example/grpc-web/ directory and run:
      dart pub get
    3. Serve the website: Compile and serve the application on port 9000 (to avoid conflict with the default gRPC server port 8080):
      webdev serve web:9000
    4. Access the app: Navigate to http://localhost:9000/ in your browser.
    docker-compose up node-server envoy commonjs-client
    dart pub get
    webdev serve web:9000
  4. Run the Route Guide sample code

    master

    The Route Guide example demonstrates unary, client streaming, server streaming, and full duplex RPCs using Dart gRPC libraries. To run the example, navigate to the example/route_guide/ directory and follow these steps:

    1. Fetch dependencies using pub get.
    2. Start the server using dart bin/server.dart.
    3. Start the client using dart bin/client.dart in a separate terminal.
    # Get dependencies
    $ pub get
    
    # Run the server
    $ dart bin/server.dart
    
    # Run the client
    $ dart bin/client.dart
  5. Explore gRPC Dart code examples

    master

    The grpc-dart repository provides several specialized examples to demonstrate different gRPC patterns and use cases:

    • Unary RPCs: See the helloworld example for a basic demonstration of performing unary calls.
    • Google APIs: See the googleapis example to learn how to communicate with Google APIs using the Dart gRPC library.
    • Metadata, Cancellation, and Timeouts: See the metadata example for handling custom metadata, managing request cancellation, and implementing timeouts.
    • Advanced RPC Patterns: See the route_guide example to learn how to implement Unary, Client Streaming, Server Streaming, and Full Duplex (Bidirectional) RPCs.
  6. Run the metadata server and client example

    master

    To run the metadata example, navigate to the example/metadata/ directory. First, fetch the necessary dependencies using dart pub get. You can then run the server and client in separate terminal sessions.

    # Get dependencies
    $ dart pub get
    
    # Run the server
    $ dart bin/server.dart
    
    # Run the client
    $ dart bin/client.dart
  7. Install the Dart protoc plugin

    master

    To enable gRPC code generation for Dart, you need to install the protoc_plugin via dart pub global activate. Ensure that ~/.pub-cache/bin is added to your system's PATH so the protoc compiler can locate the plugin.

    $ dart pub global activate protoc_plugin
  8. Run the helloworld gRPC sample code

    master

    The helloworld example demonstrates unary RPCs using Dart gRPC libraries. You can run the sample using either TCP or Unix Domain Sockets (UDS).

    TCP Setup

    1. Navigate to the example/helloworld/ directory.
    2. Install dependencies:
      dart pub get
    3. Start the server in one terminal:
      dart bin/server.dart
    4. Start the client in another terminal:
      dart bin/client.dart

    UDS Setup (*nix only)

    Note: UDS is only supported on *nix platforms.

    1. Start the server:
      dart bin/unix_server.dart
    2. Start the client:
      dart bin/unix_client.dart
    # Install dependencies
    dart pub get
    
    # Run TCP server
    dart bin/server.dart
    
    # Run TCP client
    dart bin/client.dart
  9. Regenerate Dart gRPC stubs from proto files

    master

    If you modify the service or message definitions in your .proto files, you must regenerate the Dart stubs.

    Prerequisites:

    • protoc version 3.0.0 or higher.
    • protoc_plugin (Dart plugin) version 0.7.9 or higher.

    Setup Steps:

    1. Install protoc for your OS from the Protocol Buffers releases page.
    2. Install the Dart plugin: pub global activate protoc_plugin.
    3. Ensure ~/.pub-cache/bin is in your PATH.

    Regeneration Command: Use the following command to generate files into lib/src/generated: protoc --dart_out=grpc:lib/src/generated -Iprotos protos/route_guide.proto

    # Install the Dart protoc plugin
    $ pub global activate protoc_plugin
    
    # Regenerate the Dart files
    $ protoc --dart_out=grpc:lib/src/generated -Iprotos protos/route_guide.proto
  10. Set up the grpc-web example environment

    master

    To run the grpc-web example, you need to install the webdev tool and have a gRPC-Web capable server running (such as the echo example from the grpc-web repository).

    Prerequisites

    1. Install webdev globally via Dart:
      dart pub global activate webdev
    2. Ensure you have a gRPC-Web server available. The simplest way is using Docker Compose to run the node-server, envoy, and commonjs-client services.
    dart pub global activate webdev