# Deploying Feater

This deploys Feater so you can test **video calls across 2 devices** over the internet.

## Why 3 services (not just Vercel)

Vercel runs the **Next.js frontend** only. It can't run our backend, because the
backend keeps a **persistent WebSocket (Socket.io) connection** alive to signal
WebRTC calls — Vercel's serverless functions can't do that. So:

| Part | Host | Free tier |
| --- | --- | --- |
| **Frontend** (web) | **Vercel** | yes |
| **Backend** (API + call signaling) | **Render** | yes (sleeps when idle) |
| **Database** (all your data) | **Neon** (managed Postgres) | yes |
| **Profile pics** | Backend disk (temporary) → Cloudflare R2 later | — |

### Where does my data live after deploy?

Your current data sits in **local Docker Postgres on your PC** — it does **not**
move to the cloud automatically. After deploy, all users / profiles / messages /
matches / rooms live in your **Neon cloud database**. The schema is created by
`prisma migrate deploy` (runs automatically on the backend's first boot).

> ⚠️ **Avatars (profile pics)** are saved on the backend's disk, which is **wiped
> on every redeploy** on Render's free tier. They survive while the service is up,
> which is fine for testing. For permanent storage we'll add Cloudflare R2 / S3.

---

## Step 0 — Put the code on GitHub

```bash
git init
git add .
git commit -m "Feater initial deploy"
# create an empty repo on github.com, then:
git remote add origin https://github.com/<you>/feater.git
git branch -M main
git push -u origin main
```

`.env` is git-ignored, so your local secrets are **not** pushed. Good.

---

## Step 1 — Database on Neon (free Postgres)

1. Go to **neon.tech** → sign in → **Create project**.
2. Copy the **connection string** (looks like
   `postgresql://user:pass@ep-xxx.aws.neon.tech/neondb?sslmode=require`).
3. Keep it — you'll paste it into Render as `DATABASE_URL`.

---

## Step 2 — Backend on Render

**Option A — Blueprint (uses `render.yaml`):**
1. Push to GitHub (Step 0).
2. **render.com** → **New** → **Blueprint** → pick your repo. It reads `render.yaml`.
3. When prompted, fill the secrets:
   - `DATABASE_URL` → your Neon string
   - `CORS_ORIGIN` → leave blank for now (set after Vercel gives you a URL)
4. Deploy. Wait for it to go live, e.g. `https://feater-api.onrender.com`.

**Option B — Manual web service:** New → Web Service → repo →
- Build: `npm install && npm --workspace apps/api run build`
- Start: `npm --workspace apps/api run start:prod`
- Health check path: `/api/health`
- Add the env vars from `render.yaml`.

Check it's alive: open `https://feater-api.onrender.com/api/health` → `{"status":"ok"}`.

---

## Step 3 — Frontend on Vercel

1. **vercel.com** → **Add New** → **Project** → import your repo.
2. **Root Directory** → set to **`apps/web`**. (Vercel auto-detects Next.js.)
3. **Environment Variables**:
   - `NEXT_PUBLIC_API_URL` = `https://feater-api.onrender.com`  (your Render URL)
   - (for cross-network video) `NEXT_PUBLIC_TURN_URL`, `NEXT_PUBLIC_TURN_USERNAME`,
     `NEXT_PUBLIC_TURN_CREDENTIAL` — see Step 5.
4. Deploy. You'll get e.g. `https://feater.vercel.app`.

---

## Step 4 — Connect them (CORS)

1. Back in **Render** → your service → **Environment** → set
   `CORS_ORIGIN` = `https://feater.vercel.app` (your Vercel URL, no trailing slash).
2. Save → Render redeploys. Now the browser is allowed to call the API + open the
   socket.

---

## Step 5 — TURN server (needed for 2 different networks)

STUN (built in) connects two devices on the **same wifi**. For a phone on mobile
data calling a laptop on home wifi, you need **TURN**:

1. Sign up free at **metered.ca** (or Twilio) → get TURN credentials.
2. In **Vercel** env vars add:
   - `NEXT_PUBLIC_TURN_URL` = `turn:global.relay.metered.ca:80,turns:global.relay.metered.ca:443`
   - `NEXT_PUBLIC_TURN_USERNAME` = (from metered)
   - `NEXT_PUBLIC_TURN_CREDENTIAL` = (from metered)
3. Redeploy the Vercel project.

---

## Step 6 — Demo accounts (log in without email)

Production won't show the dev OTP code, and email needs SMTP. The easiest way to
test is the **seeded demo accounts** (email + password login, no OTP):

Run once against your Neon DB (locally, with the Neon URL):
```bash
cd apps/api
DATABASE_URL="<your-neon-url>" npm run prisma:seed
```
Now log in on both devices with e.g. `demo@feater.app` / `Password123` and
`aarav@feater.app` / `Password123`, make them friends, and call.

(To use real signups instead, set the `SMTP_*` env vars on Render — Gmail app
password works — and OTP emails will be delivered.)

---

## Step 7 — Test the video call

1. Device A → open `https://feater.vercel.app` → log in as `demo@feater.app`.
2. Device B → log in as `aarav@feater.app`.
3. Make them friends (Discover → like each other), open Chats → tap the video icon.
4. Allow camera/mic on both. 🎥

---

## Notes & gotchas

- **Render free tier sleeps** after ~15 min idle; the first request cold-starts
  (~30–60s). Just wait on first load.
- **HTTPS is required** for camera/mic — Vercel + Render both give HTTPS, so good.
- **Avatars reset on redeploy** (Render free disk is ephemeral). Add R2/S3 for
  permanence later.
- Redis isn't required yet (not on the critical path).
