Skip to main content

Overview

The xmtp_api_grpc crate provides a gRPC implementation of the XmtpApi trait. It supports both native (HTTP/2) and WebAssembly (gRPC-Web) environments through platform-specific service types.

Source Location

crates/xmtp_api_grpc/

GrpcClient

The main gRPC client implementation that works across native and WASM environments.

Platform-Specific Services

Native (HTTP/2)
WebAssembly (gRPC-Web)

Construction

Basic Creation
Creates a gRPC client with the specified host and TLS setting. With App Version
Creates a gRPC client with application version metadata attached to all requests. Builder Pattern
Returns a ClientBuilder for more flexible configuration.

ClientBuilder

A fluent builder for constructing GrpcClient instances with custom configuration.

Configuration Methods

Network Configuration
  • set_host(host: String) - Set the gRPC server host URL
  • set_tls(tls: bool) - Enable/disable TLS
  • rate_per_minute(limit: u32) - Set rate limiting
  • port() -> Result<Option<String>, GrpcBuilderError> - Get port from configured host
Version Configuration
  • set_app_version(version: AppVersion) -> Result<(), GrpcBuilderError> - Set application version
  • set_libxmtp_version(version: String) -> Result<(), GrpcBuilderError> - Set libxmtp version
Retry Configuration
  • set_retry(retry: Retry) - Configure retry strategy
Build
  • build() -> Result<GrpcClient, GrpcBuilderError> - Construct the client

Example

Client Implementation

The GrpcClient implements the Client trait from xmtp_proto::api_client, providing:

Request Method

Sends a unary gRPC request and returns the response.

Stream Method

Initiates a server-streaming gRPC request and returns a stream of responses.

Fake Stream Method

Creates an empty stream for testing purposes.

GrpcStream

A stream of bytes from a gRPC network source.
Implements Stream<Item = Result<Bytes, GrpcError>> for asynchronous message streaming.

Connection Health Check

Checks if the gRPC connection is ready to accept requests.

Error Types

GrpcBuilderError

Errors that can occur during client construction:

GrpcError

Errors that can occur during gRPC operations:
Implements RetryableError - all gRPC errors are considered retryable by default.

Request Metadata

All requests automatically include these metadata headers:
  • x-app-version - Application version from builder configuration
  • x-libxmtp-version - LibXMTP version (defaults to crate version)

Message Size Limits

The client enforces payload size limits defined in xmtp_configuration::GRPC_PAYLOAD_LIMIT for both encoding and decoding.

Streaming Support

The crate includes several stream utilities in the streams module:
  • EscapableTonicStream - Stream wrapper for handling tonic responses
  • FakeEmptyStream - Empty stream for testing
  • NonBlockingWebStream - Non-blocking stream adapter for WASM
  • MultiplexedStream - Multiplexed stream handling

Platform Compatibility

Native (not WASM)
  • Uses HTTP/2 transport
  • Full tonic transport features
  • TCP connection pooling
WebAssembly
  • Uses gRPC-Web protocol
  • HTTP/1.1 compatibility
  • Browser fetch API integration
  • xmtp_api - Core trait definitions
  • xmtp_proto - Protocol buffer definitions
  • tonic - gRPC framework
  • xmtp_configuration - Configuration constants