Presence trait provides methods for managing your online/offline status and subscribing to contact presence updates.
Access
Access presence operations through the client:Methods
set
Set your presence status (online or offline).status: PresenceStatus- EitherAvailable(online) orUnavailable(offline)
- Push name must be set before sending presence
- Returns error if push name is empty
set_available
Convenience method to set status to available (online).set_unavailable
Convenience method to set status to unavailable (offline).subscribe
Subscribe to a contact’s presence updates.jid- Contact JID to subscribe to
- Sends a
<presence type="subscribe">stanza - Automatically includes TC token if available for the contact
- Used to receive notifications when the contact goes online/offline
PresenceStatus Enum
as_str()- Returns"available"or"unavailable"
Push Name Requirement
WhatsApp requires a push name (display name) to be set before sending presence updates. This matches WhatsApp Web behavior. Error example:Wire Format
Setting Presence
Subscribing to Presence
TC Token Handling
When subscribing to presence, the library automatically:- Looks up TC token for the target JID
- Includes token as child node if available
- Skips token if not found (non-error)
Behavior Notes
Available (Online)
When setting status toAvailable, the library automatically:
- Validates push name is set
- Sends unified session (internal protocol requirement)
- Broadcasts presence stanza with push name
Unavailable (Offline)
When setting status toUnavailable:
- Validates push name is set
- Broadcasts unavailable presence
Error Handling
All methods returnResult<(), anyhow::Error>. Common errors:
- Push name not set: “Cannot send presence without a push name set”
- Not connected: Connection-related errors
- Network errors: Timeout, connection lost, etc.