Overview
WhatsApp-Rust is a high-performance, async Rust library for the WhatsApp Web API. The project follows a modular, layered architecture that separates protocol concerns from runtime concerns, enabling platform-agnostic core logic with pluggable backends.Workspace Structure
The project is organized as a Cargo workspace with multiple crates:Three Main Crates
wacore - Platform-Agnostic Core
Location:wacore/
Purpose: Contains core logic for the WhatsApp binary protocol, cryptography primitives, IQ protocol types, and state management traits.
Key Features:
- No runtime dependencies on Tokio or specific databases
- Pure protocol implementation
- Cryptographic operations (Signal Protocol, Noise Protocol)
- Type-safe protocol node builders
waproto - Protocol Buffers
Location:waproto/
Purpose: Houses WhatsApp’s Protocol Buffers definitions compiled to Rust structs.
Build Process:
Message- All message typesWebMessageInfo- Message metadataHistorySync- Chat historySyncActionValue- App state mutations
whatsapp-rust - Main Client
Location:src/
Purpose: Integrates wacore with the Tokio runtime, provides high-level client API, and manages storage.
Key Features:
- Asynchronous operations with Tokio
- SQLite persistence (pluggable)
- Event bus system
- Feature modules (groups, media, etc.)
Key Components
Client
Location:src/client.rs
Purpose: Orchestrates connection lifecycle, event bus, and high-level operations.
- Connection management
- Request/response routing
- Event dispatching
- Session management
PersistenceManager
Location:src/store/persistence_manager.rs
Purpose: Manages all state changes and persistence.
- Never modify
Devicestate directly - Use
DeviceCommand+process_command() - For read-only:
get_device_snapshot()
Signal Protocol
Location:wacore/libsignal/ & src/store/signal*.rs
Purpose: End-to-end encryption via Signal Protocol implementation.
Features:
- Double Ratchet algorithm
- Pre-key bundles
- Session management
- Sender keys for groups
Socket & Handshake
Location:src/socket/, src/handshake.rs
Purpose: WebSocket connection and Noise Protocol handshake.
Flow:
- WebSocket connection
- Noise handshake (XX pattern)
- Encrypted frame exchange
Module Interactions
Layer Responsibilities
wacore Layer (Platform-Agnostic)
- Protocol logic
- State traits
- Cryptographic helpers
- Data models
whatsapp-rust Layer (Runtime)
- Runtime orchestration
- Storage integration
- User-facing API
Protocol Entry Points
Incoming Messages
Flow:src/message.rs → Signal decryption → Event dispatch
Outgoing Messages
Flow:src/send.rs → Signal encryption → Socket send
Socket Communication
Flow:src/socket/ → Noise framing → Transport
Connection Lifecycle
Auto-Reconnection
The client implements robust reconnection handling:- Connection lost →
cleanup_connection_state() - Check
enable_auto_reconnect→ exit if disabled - Check
expected_disconnect→ immediate reconnect if expected (e.g., 515) - Calculate backoff delay (2s × error_count, max 30s)
- Wait → attempt reconnection
Keepalive Loop
Monitors connection health with periodic pings:- Sends ping every 20-30 seconds (randomized)
- Waits up to 20s for response
- Forces reconnect after 180s of consecutive failures
Offline Sync
When reconnecting, the client tracks offline message sync progress:- Receive
<ib><offline_preview count="N"/>→ start tracking - Process messages with
offlineattribute → increment counter - Receive
<ib><offline/>→ sync complete - Emit
OfflineSyncCompletedevent
Concurrency Patterns
Per-Chat Message Queues
Prevents race conditions where a later message is processed before the PreKey message:Per-Device Session Locks
Prevents concurrent Signal protocol operations on the same session:Background Saver
Periodic persistence with dirty flag optimization:Feature Organization
Location:src/features/