Overview
Thexmtp_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)Construction
Basic CreationClientBuilder for more flexible configuration.
ClientBuilder
A fluent builder for constructingGrpcClient instances with custom configuration.
Configuration Methods
Network Configurationset_host(host: String)- Set the gRPC server host URLset_tls(tls: bool)- Enable/disable TLSrate_per_minute(limit: u32)- Set rate limitingport() -> Result<Option<String>, GrpcBuilderError>- Get port from configured host
set_app_version(version: AppVersion) -> Result<(), GrpcBuilderError>- Set application versionset_libxmtp_version(version: String) -> Result<(), GrpcBuilderError>- Set libxmtp version
set_retry(retry: Retry)- Configure retry strategy
build() -> Result<GrpcClient, GrpcBuilderError>- Construct the client
Example
Client Implementation
TheGrpcClient implements the Client trait from xmtp_proto::api_client, providing:
Request Method
Stream Method
Fake Stream Method
GrpcStream
A stream of bytes from a gRPC network source.Stream<Item = Result<Bytes, GrpcError>> for asynchronous message streaming.
Connection Health Check
Error Types
GrpcBuilderError
Errors that can occur during client construction:GrpcError
Errors that can occur during gRPC operations: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 configurationx-libxmtp-version- LibXMTP version (defaults to crate version)
Message Size Limits
The client enforces payload size limits defined inxmtp_configuration::GRPC_PAYLOAD_LIMIT for both encoding and decoding.
Streaming Support
The crate includes several stream utilities in thestreams module:
EscapableTonicStream- Stream wrapper for handling tonic responsesFakeEmptyStream- Empty stream for testingNonBlockingWebStream- Non-blocking stream adapter for WASMMultiplexedStream- Multiplexed stream handling
Platform Compatibility
Native (not WASM)- Uses HTTP/2 transport
- Full tonic transport features
- TCP connection pooling
- Uses gRPC-Web protocol
- HTTP/1.1 compatibility
- Browser fetch API integration
Related Crates
xmtp_api- Core trait definitionsxmtp_proto- Protocol buffer definitionstonic- gRPC frameworkxmtp_configuration- Configuration constants
