# Real-time campaign progress and database notifications

Reverb and Laravel Echo are installed. Campaign send results, delivery/read receipts, counters, and status changes emit private-channel refresh events after database commit. The send page and delivery-status relation manager reload persisted data when an event arrives. Filament's notification bell listens on the signed-in user's private channel, including when its list is empty. Any Filament-format database notification triggers an event after storage; no special flag is needed at each notification call site. Existing toast-only notifications remain temporary.

Both interfaces retain 30-second polling to recover from socket outages or missed events. Reconnection also refreshes them. Broadcast payloads contain only the campaign ID or an empty notification invalidation, never recipient/message data or notification contents. Campaign authorization uses the same view permission as the panel; user notifications can only be subscribed to by their owner.

## Production configuration

Generate separate Reverb credentials once and store them in the server `.env`:

```sh
php -r 'echo bin2hex(random_bytes(16)), PHP_EOL;'
```

Run that command separately for the app ID and key, and use `random_bytes(32)` for the secret. Keep the values stable across deployments. Do not change Laravel's `APP_KEY`.

```dotenv
BROADCAST_CONNECTION=reverb
QUEUE_CONNECTION=database
CACHE_STORE=database
REVERB_APP_ID=YOUR_GENERATED_ID
REVERB_APP_KEY=YOUR_GENERATED_KEY
REVERB_APP_SECRET=YOUR_GENERATED_SECRET
REVERB_SERVER_HOST=127.0.0.1
REVERB_SERVER_PORT=8080

# Laravel publishes internally without TLS.
REVERB_HOST=127.0.0.1
REVERB_PORT=8080
REVERB_SCHEME=http

# Browsers connect through your public HTTPS reverse proxy.
REVERB_PUBLIC_HOST=ws.example.com
REVERB_PUBLIC_PORT=443
REVERB_PUBLIC_SCHEME=https
REVERB_ALLOWED_ORIGINS=campaigns.example.com
```

Replace both example hostnames. `REVERB_ALLOWED_ORIGINS` accepts a comma-separated list of application hostnames without schemes; it defaults to the hostname in `APP_URL`. Echo gets its public settings from the rendered authenticated page, so GitHub Actions does not need `VITE_REVERB_*` variables or the Reverb secret to build assets.

## Supervisor and reverse proxy

Copy `deploy/supervisor/platform-whatsapp.conf.example` into `/etc/supervisor/conf.d/platform-whatsapp.conf`, replacing the directory, cPanel Linux user, and PHP executable. The queue worker must consume **broadcasts,campaigns,default**, with broadcasts first. An old worker consuming only campaigns/default will leave live updates queued. The worker timeout is 60 seconds; queue `retry_after` must exceed that (the default is 90 seconds).

Configure DNS and a valid TLS certificate for `ws.example.com`. Inside its HTTPS Nginx virtual host, forward all paths (including `/app` and `/apps`) to Reverb:

```nginx
location / {
    proxy_pass http://127.0.0.1:8080;
    proxy_http_version 1.1;
    proxy_set_header Host $host;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_read_timeout 3600;
    proxy_send_timeout 3600;
}
```

On managed cPanel hosting, your provider may need to configure Supervisor and this proxy. Reverb binds to loopback; port 8080 does not need public exposure. HTTPS application pages require the public WSS endpoint above, not an unsecured `ws://` connection.

After deployment:

```sh
php artisan config:cache
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl status
php artisan queue:restart
php artisan reverb:restart
```

The existing Actions deployment builds the JS and restarts queue workers/Reverb. Supervisor automatically starts them again. On initial installation, create the Supervisor programs first. Keep the application's existing scheduler cron job.

## Local development and checks

Local `.env` is configured with generated Reverb credentials, internal port 8080, and the trusted Herd WSS proxy `platform-whatsapp-ws.test:443`. The proxy was created with:

```sh
herd proxy platform-whatsapp-ws http://127.0.0.1:8080 --secure
```

Start a worker consuming `broadcasts,campaigns,default` and `php artisan reverb:start`; `composer dev` also includes these processes. On another development machine, create the proxy and set `REVERB_PUBLIC_HOST=platform-whatsapp-ws.test`, `REVERB_PUBLIC_PORT=443`, and `REVERB_PUBLIC_SCHEME=https`.

```sh
php artisan test --compact tests/Feature/RealtimeUpdatesTest.php
npm run test:realtime
npm run build
```

In the browser, verify a WebSocket handshake (`101`) and a successful `/broadcasting/auth` response. A second authenticated browser should update when a message changes or a database notification is stored. Real WhatsApp sending and remote production TLS are separate checks from local Reverb transport tests.
