# Host WhatsApp Node on Render (before going live)

Use [Render Dashboard](https://dashboard.render.com/) for the **Node bot only** (`services/whatsapp-node`). Keep Laravel on cPanel (or elsewhere). Laravel’s `node_base_url` will be your Render HTTPS URL.

> **CRITICAL — ONE instance only per WhatsApp session.**
> A WhatsApp session may run on **exactly one host at a time**. If you deploy on Render, **fully stop any other bot first** (and vice versa). Running two hosts with the same session auth causes delivery failures.
>
> - Render **OR** another host, never both connected to the same account.
> - When migrating: stop the old host, copy `.wwebjs_auth/` once, then start Render (re-link the device if needed).
> - Keep a **single** Render instance (do not scale the web service to 2+ instances) — each instance would open its own socket to the same session.
> - Verify with `GET /` / `GET /health`: two different `instance_id`s answering for one account = dual-run, stop one.

## Important Render choices

| Setting | Recommendation |
|---------|----------------|
| Service type | **Web Service** (not Static, not Cron) |
| Plan | **Starter** or higher — Free tier sleeps when idle and **kills WhatsApp Web sessions** |
| Persistent disk | **Required** — mount to `.wwebjs_auth` (QR sessions must survive restarts) |
| Region | Closest to you / your Laravel host |

## Step-by-step (Dashboard UI)

### 1. Push the repo to GitHub/GitLab

Render deploys from git. Ensure `services/whatsapp-node/` is in the repo (do **not** commit `node_modules`).

### 2. New Web Service

1. Open [dashboard.render.com](https://dashboard.render.com/) → **New** → **Web Service**
2. Connect the repo
3. Configure:

| Field | Value |
|-------|--------|
| Name | `cps-whatsapp-node` |
| Root Directory | `services/whatsapp-node` |
| Runtime | Node |
| Build Command | `npm install --omit=dev` |
| Start Command | `node app.js` |
| Instance type | Starter+ (avoid Free for production WhatsApp) |

### 3. Environment

| Key | Value |
|-----|--------|
| `NODE_VERSION` | `20` |
| `HOST` | `0.0.0.0` |
| `DATA_DIR` | `/var/data` |

Render sets `PORT` automatically — do not hardcode it.

### 4. Disk (sessions)

In the service → **Disks** → Add disk:

- Name: `wa-sessions`
- Mount path: `/var/data`
- Size: 1 GB is enough to start

Without this, every deploy/restart forces users to re-scan QR.

### 5. Health check

- Path: `/health`  
- Expect JSON: `{"ok":true,"service":"cps-whatsapp-node",...}`

### 6. Deploy

Click **Create Web Service** / **Deploy**. Wait until status is **Live**.

Open:

`https://YOUR-SERVICE.onrender.com/`

You should see `ok: true`. (`passenger` will be `false` on Render — that is normal.)

### 7. Point Laravel at Render

Owner → Settings → WhatsApp:

- Enable integration  
- Node base URL: `https://YOUR-SERVICE.onrender.com` (no trailing slash)  
- **Ping node**  
- Then Devices → Start/QR or Pair → test send  

## Optional Blueprint

[`render.yaml`](render.yaml) in this folder can be used with Render Blueprint if you prefer YAML over the UI. Adjust `region` / `plan` as needed.

## After it is live

1. Do not rotate/redeploy carelessly without the disk attached.  
2. Keep Laravel scheduler running (`whatsapp:ping-node`) so the service stays warm.  
3. If you later move Laravel to Render too, that is a separate PHP/Docker service — this guide is only for the Node bot.

## Local check before first Render deploy

```bash
cd services/whatsapp-node
npm install
set HOST=0.0.0.0
set PORT=3000
node app.js
# open http://127.0.0.1:3000/health
```
