# CPS WhatsApp Node

Self-hosted WhatsApp for CPS Laravel. **Default engine: Baileys 7** (cPanel-friendly, MPWA-style). Optional: **whatsapp-web.js** for local Business testing.

## Engines

| `WA_ENGINE` | Engine | Best for |
|-------------|--------|----------|
| `baileys7` (default) | Baileys 7 | **cPanel Node**, VPS, low RAM |
| `webjs` | whatsapp-web.js + Chromium | Local dev, Business delivery tests |

## Local development

**Recommended (repo root, MPWA-style):**

```powershell
cd d:\RABI\rabi\saas
npm install
npm start
# or: node server.js
```

Uses `.env` vars: `PORT_NODE`, `WA_ENGINE`, `WA_URL_SERVER`. Auth stored in `storage/whatsapp-node/`.

**Alternative (subfolder only):**

```powershell
cd services/whatsapp-node
npm install

# Default Baileys 7 (production-like)
$env:HOST="127.0.0.1"; $env:PORT="3000"; node app.js

# Optional: Chromium engine for Business test
$env:WA_ENGINE="webjs"; node app.js
```

Auth stored in `services/whatsapp-node/` when `DATA_DIR` is not set.

Health: `GET http://127.0.0.1:3000/health` → `engine: baileys7` or `whatsapp-web-js`

Laravel **Node base URL** = `http://127.0.0.1:3000` (or `WA_URL_SERVER` in `.env`)

## Auth storage

| Engine | Folder (root `server.js`) | Folder (subfolder `app.js`) |
|--------|---------------------------|-----------------------------|
| baileys7 | `storage/whatsapp-node/baileys_credentials/session-{api_key}/` | `{DATA_DIR or app folder}/baileys_credentials/...` |
| webjs | `storage/whatsapp-node/.wwebjs_auth/session-{api_key}/` | `{DATA_DIR or app folder}/.wwebjs_auth/...` |

Device delete in Laravel → `GET /logout/{api_key}` destroys session + wipes auth.

## Socket.IO (live QR)

When Node runs standalone (not Passenger), Socket.IO is enabled on the same port. The device page connects for instant QR updates. HTTP polling still works as fallback.

Events: `StartConnection`, `qrcode`, `connection-open`, `LogoutDevice`

## cPanel deploy

**Recommended (CloudLinux):** [DEPLOY-SPLIT.md](../../DEPLOY-SPLIT.md) — upload **this folder only** to `wa-node/`, startup **`app.js`**.

Single-repo alternative: [CPANEL-DEPLOY.md](../../CPANEL-DEPLOY.md) (root `server.js`).

Standalone Node: [CPANEL-DEPLOY.md](./CPANEL-DEPLOY.md)

## VPS deploy

See [VPS-DEPLOY.md](./VPS-DEPLOY.md)

## API (unchanged — Laravel WhatsAppNodeClient)

| Method | Path |
|--------|------|
| GET | `/health` |
| GET | `/start/:sessionId` |
| POST | `/start-pairing/:sessionId` |
| GET | `/status/:sessionId` |
| GET | `/send-message` |
| POST | `/send-media` |
| GET | `/logout/:sessionId` |

## Environment

| Variable | Default | Purpose |
|----------|---------|---------|
| `WA_ENGINE` | `baileys7` | `baileys7` or `webjs` |
| `HOST` | `0.0.0.0` | Bind address |
| `PORT` / `PORT_NODE` | `3000` | HTTP + Socket.IO |
| `DATA_DIR` | app folder or `storage/whatsapp-node` (via root `server.js`) | Data root |
| `BAILEYS_AUTH_DIR` | `{DATA_DIR}/baileys_credentials` | Baileys sessions |
| `WWEBJS_AUTH_DIR` | `{DATA_DIR}/.wwebjs_auth` | web.js sessions |
| `WA_URL_SERVER` | — | Laravel default Node URL (`.env`) |
