Overview
The transport layer provides a runtime-agnostic abstraction for network connections. It handles raw byte transmission without knowledge of WhatsApp’s protocol framing. The transport system consists of two main traits:- Transport - Represents an active connection for sending/receiving raw bytes
- TransportFactory - Creates new transport instances and event streams
Transport Trait
TheTransport trait represents an active network connection as a simple byte pipe.
Methods
send
Sends raw bytes through the transport. The caller is responsible for any protocol framing.data- Raw bytes to send
Ok(())on successErr(anyhow::Error)on failure
disconnect
Gracefully closes the connection.TransportFactory Trait
Creates new transport instances and associated event streams.Methods
create_transport
Establishes a new connection and returns both the transport handle and an event receiver.Arc<dyn Transport>- The transport instance for sending dataasync_channel::Receiver<TransportEvent>- Stream of transport events
TransportEvent
Events produced by the transport layer:Event Types
Connected
Emitted immediately after successful connection establishment.DataReceived
Emitted when raw data is received from the server.bytes: Bytes- Raw data received (from thebytescrate)
Disconnected
Emitted when the connection is closed (gracefully or due to error).TokioWebSocketTransport
The default implementation using tokio-websockets for async WebSocket connections.Features
- Async I/O - Built on Tokio runtime
- TLS Support - Uses rustls with webpki-roots for certificate validation
- Split Architecture - Separate read/write paths for efficiency
- Automatic Reconnection - Handled by higher-level Client code
- Development Mode - Optional
danger-skip-tls-verifyfeature
Creating a Transport Factory
Usage with Client
TLS Configuration
By default, TokioWebSocketTransport validates TLS certificates using webpki-roots:Development Mode (Skip TLS Verification)
WARNING: Only use for development/testing! Enable thedanger-skip-tls-verify feature to disable certificate verification:
Connection Settings
The default WebSocket URL is:Internal Architecture
TokioWebSocketTransport splits the WebSocket into separate read/write paths:Implementing Custom Transports
You can implement custom transports for different runtimes or protocols.Example: Mock Transport for Testing
Example: TCP Transport (No TLS)
Best Practices
- Thread Safety - Transport must be
Send + Sync - Error Handling - Return descriptive errors from
send() - Graceful Shutdown - Implement proper cleanup in
disconnect() - Event Channel Size - Use bounded channels with reasonable capacity
- Read Task - Spawn a separate task for receiving data
- Resource Cleanup - Ensure sockets/resources are closed on drop
Testing Transports
Unit Test Example
See Also
- Storage Traits - Storage backend abstraction
- HTTP Client Trait - HTTP client abstraction
- Client API - Main client interface