Skip to content

Running in Production

A WhatsApp Web client is a long-lived process that owns a browser and a linked device. Running it well comes down to four things: keep the session on durable storage, run exactly one process per account, restart it when it dies, and watch its health.

  • Install the PAM runtime and Google Chrome (or Chromium if you do not send video). Set PAM_CHROME_PATH or browserExecutable if Chrome is not on the PATH.
  • Store sessions on a persistent disk, or use RemoteAuth when containers are ephemeral.
  • Restrict the session directory to the service user. LocalAuth creates it with 0700 permissions.
  • Link the device once from a trusted terminal (QR or pairing code), then start the service.
  • Run one process per account. Two processes sharing a profile directory corrupt the session, and two sessions of the same account conflict.

Automatic reconnection is off by default. Turn it on to relaunch the browser when the session drops because of a conflict or because WhatsApp Web unloaded:

use Pam\WhatsApp\ClientOptions;
$client = new Client(new ClientOptions(
authStrategy: $auth,
autoReconnect: true,
reconnectMaxAttempts: 5, // 1 to 10, default 3
reconnectDelayMs: 2_000, // default 1000
));

Only DisconnectionReason::Conflict and DisconnectionReason::Unlaunched are reconnected automatically. A logout or an exhausted QR retry limit never is, because both need a human to link the device again. When all attempts fail, the client moves to Failed and throws OperationException.

After the client has closed or failed, you can also schedule a reconnect yourself with $client->reconnect() and keep pumping.

diagnoseSession() returns a SessionDiagnostics snapshot that is cheap to expose on a health endpoint or log periodically:

$diagnostics = $client->diagnoseSession();
if (!$diagnostics->healthy()) {
error_log(json_encode([
'state' => $diagnostics->clientState->name,
'connection' => $diagnostics->connectionState?->name,
'browser' => $diagnostics->browserAvailable,
'errors' => $diagnostics->errors,
]));
}

healthy() is true only when the client is Ready, the browser is available, the connection state is Connected, and no probe failed. The snapshot also includes webVersion and accountId.

Pass a logger closure to receive structured log lines for initialization, retries, and reconnects:

use Pam\WhatsApp\LogLevel;
$client = new Client(new ClientOptions(
logger: static function (LogLevel $level, string $message, array $context): void {
fwrite(STDERR, sprintf("[%s] %s %s\n", $level->name, $message, json_encode($context)));
},
));

LogLevel has the cases Debug, Info, Warning, and Error.

Run the listener under a supervisor that restarts it on exit. A minimal systemd unit:

/etc/systemd/system/acme-whatsapp.service
[Unit]
Description=Acme WhatsApp listener
After=network-online.target
[Service]
User=acme
WorkingDirectory=/srv/acme
ExecStart=/usr/local/bin/pam listen.php
Restart=on-failure
RestartSec=10
KillSignal=SIGTERM
[Install]
WantedBy=multi-user.target

Handle SIGTERM in your script so Chrome is closed before the process exits; see Stopping cleanly. Adjust the path to the pam binary to match your installation.

Option Default Description
browserExecutable null Path to Chrome; auto-detected when null.
headless true Run Chrome without a window.
browserTimeoutSeconds 30.0 Time allowed for Chrome to start.
browserArguments [] Extra Chrome flags, for example --proxy-server=....
proxyAuthentication null new ProxyAuthentication($username, $password) for an authenticated proxy.
userAgent null Override the browser user agent.
puppeteer null A Pam\Browser\Chromium\LaunchOptions; when set, it replaces the browser options above.

WhatsApp ships new web builds often. The client pins a known web build through webVersion and, with the default webVersionCache (LocalWebCacheOptions), keeps a copy of that build’s HTML in ./.wwebjs_cache so restarts load the same version:

use Pam\WhatsApp\LocalWebCacheOptions;
use Pam\WhatsApp\NoWebCacheOptions;
use Pam\WhatsApp\RemoteWebCacheOptions;
new ClientOptions(webVersionCache: new LocalWebCacheOptions(path: '/srv/acme/wwebjs-cache'));
new ClientOptions(webVersionCache: new RemoteWebCacheOptions('https://cdn.example.com/wa/{version}.html'));
new ClientOptions(webVersionCache: new NoWebCacheOptions());
new ClientOptions(webVersion: null); // always load the current build

When WhatsApp retires a build, the web client may refuse to load it. Upgrade the package to pick up a newer pinned version and the bridge changes that come with it.

Each client runs its own Chrome process, so memory grows with every account you add. Size your server for the number of accounts, keep automatic media downloads off unless you need them (setAutoDownloadPhotos(false) and friends), and stream large downloads with downloadMediaStream().