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
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/
State Management Flow
Best Practices
State Management
Async Operations
Error Handling
Related Sections
Authentication
Learn about QR code and pair code flows
Events
Understand the event system and handlers
Storage
Explore storage backends and state management
Getting Started
Build your first WhatsApp bot