Overview
WhatsApp-Rust uses a layered storage architecture with pluggable backends. ThePersistenceManager manages all state changes, while the Backend trait defines storage operations for device data, Signal protocol keys, app state sync, and protocol-specific data.
Architecture
PersistenceManager
Location:src/store/persistence_manager.rs
Purpose
Manages all device state changes and persistence operations. Acts as the gatekeeper for state modifications.Structure
device: In-memory device state (protected by RwLock)backend: Storage backend implementationdirty: Flag indicating unsaved changessave_notify: Notification channel for background saver
Initialization
Key Methods
get_device_snapshot
Purpose: Read-only access to device statemodify_device
Purpose: Modify device state with automatic dirty trackingprocess_command
Purpose: Apply state changes viaDeviceCommand
Background Saver
Purpose: Periodically persist dirty state to disk- Wakes up when notified or after interval
- Only saves if dirty flag is set
- Uses optimistic locking (dirty flag)
Backend Trait
Location:wacore/src/store/traits.rs
Overview
TheBackend trait is automatically implemented for any type that implements all four domain-specific traits:
Domain Traits
SignalStore
Purpose: Signal protocol cryptographic operationsAppSyncStore
Purpose: WhatsApp app state synchronizationcritical_block- Blocked contactsregular- Chat settings (pin, mute, archive)regular_high- Contact info, push namesregular_low- Chat messages metadata
ProtocolStore
Purpose: WhatsApp Web protocol-specific storageDeviceStore
Purpose: Device data persistenceSqliteStore Implementation
Location:storages/sqlite-storage/src/lib.rs
Database Schema
Multi-Account Support
Each device has uniquedevice_id:
device_id:
DeviceCommand Pattern
Location:src/store/commands.rs, wacore/src/store/commands.rs
Purpose
Provide type-safe, centralized state mutations.Command Enum
Command Application
Usage
State Management Best Practices
Read-Only Access
Modifications
Bulk Operations
Critical Errors
Custom Backend Implementation
Example: PostgreSQL Backend
Usage
Migration & Debugging
Database Snapshots
Feature flag:debug-snapshots
Related Sections
Architecture
Understand PersistenceManager’s role
Authentication
Learn how session data is persisted
Custom Backends
Implement your own storage backend
Storage API
Complete storage API reference