Skip to content

Events

Everything that happens in the linked WhatsApp session reaches PHP as a typed, immutable event object. Register listeners before calling initialize(), then let run() deliver events for as long as the client is alive.

use Pam\WhatsApp\Event\MessageReceived;
use Pam\WhatsApp\Event\CallReceived;
use Pam\WhatsApp\EventType;
$client->onMessage(static function (MessageReceived $event): void {
// ...
});
$client->on(EventType::CallReceived, static function (CallReceived $event): void {
$event->call->reject();
});
$client->initialize();
$client->run();

Shortcut methods exist for the most common events, and on() accepts any EventType. All of them return the client, so you can chain them:

Shortcut Equivalent
onQrCode() on(EventType::QrCode, ...)
onReady() on(EventType::Ready, ...)
onMessage() on(EventType::MessageReceived, ...)
onMessageSent(), onMessageDelivered(), onMessageRead(), onMessageFailed() Filters of EventType::MessageAcknowledged by delivery stage. See delivery tracking.

Every listener receives one object from the Pam\WhatsApp\Event namespace (or a domain object where noted).

EventType Object Properties
QrCode QrCodeReceived code
PairingCodeReceived PairingCodeReceived code
Authenticated Authenticated timestamp, authPayload
AuthenticationFailure ClientError message
Ready Ready timestamp
LoadingScreen LoadingScreen percent, message
StateChanged ConnectionStateChanged state (ConnectionState)
Disconnected Disconnected reason (DisconnectionReason: Logout, Conflict, Unlaunched, QrRetryLimit)
Error ClientError message
RemoteSessionSaved RemoteSessionSaved timestamp
BatteryChanged BatteryChanged battery, plugged
EventType Object Properties
MessageReceived MessageReceived message
MessageAcknowledged MessageAcknowledged message, ack (MessageAck)
MessageCreated MessageLifecycle type, message, context
MessageEdited MessageLifecycle type, message, context
MessageRevokedEveryone, MessageRevokedMe MessageLifecycle type, message, context
MessageCiphertext, MessageCiphertextFailed, MediaUploaded MessageLifecycle type, message, context
MessageReaction Pam\WhatsApp\Reaction reaction, senderId, msgId, timestamp
VoteUpdated Pam\WhatsApp\PollVote voter, selectedOptions, parentMessage

MessageReceived fires for messages from other people. MessageCreated also fires for messages the linked account sends, including from the phone, which makes it the right hook for logging every message in a chat.

EventType Object Properties
ChatArchived ChatArchiveChanged chatId, archived, previousArchived
ChatRemoved ChatRemoved chatId
UnreadCountChanged UnreadCountChanged chatId, unreadCount
ContactChanged ContactChanged message, oldId, newId, isContact
CallReceived CallReceived call (with reject()), id, peerId, isVideo, isGroup

GroupJoined, GroupLeft, GroupAdminChanged, GroupUpdated, and GroupMembershipRequest all deliver a GroupNotification:

use Pam\WhatsApp\Event\GroupNotification;
use Pam\WhatsApp\EventType;
$client->on(EventType::GroupJoined, static function (GroupNotification $event): void {
$count = count($event->recipientIds);
$event->reply("Welcome to the group, {$count} new member(s)! Please read the pinned rules.");
});

A GroupNotification has type (GroupNotificationType: Add, Invite, Remove, Leave, Promote, Demote, Subject, Description, Picture, Announce, Restrict), author, body, chatId, recipientIds, and timestamp, plus getChat(), getContact(), getRecipients(), and reply().

run() blocks and pumps events until the client state becomes Closed or Failed, which happens after destroy(), logout(), a disconnection, or an unrecoverable error.

For more control, drive the loop yourself with pump(). It waits up to the given number of seconds for bridge events, handles them, and returns whether anything was handled:

use Pam\WhatsApp\ClientState;
$client->initialize();
while (!in_array($client->state, [ClientState::Closed, ClientState::Failed], true)) {
$client->pump(1.0);
// Do other periodic work here, such as draining an outbound queue.
}

run() does not install signal handlers. To close Chrome when the process receives Ctrl+C or SIGTERM, drive the loop yourself and close the client when a signal arrives:

listen.php
$stop = false;
pcntl_async_signals(true);
pcntl_signal(SIGINT, static function () use (&$stop): void { $stop = true; });
pcntl_signal(SIGTERM, static function () use (&$stop): void { $stop = true; });
$client->initialize();
try {
while (!$stop && !in_array($client->state, [ClientState::Closed, ClientState::Failed], true)) {
$client->pump(1.0);
}
} finally {
$client->destroy(); // keeps the saved session for the next start
}

The PCNTL functions are available in the PAM runtime.