# Unique ID service – domain mapping

The WebSocket server and API server can be mapped to different domains/hosts using environment variables.

## Environment variables

| Variable | Default | Used by | Description |
|----------|---------|---------|-------------|
| `WEBSOCKET_HOST` | `10.0.0.109` | Both | Host where the WebSocket server listens (and that the API connects to). |
| `WEBSOCKET_PORT` | `7845` | Both | WebSocket server port. |
| `WEBSOCKET_PATH` | `voicebot_session_2002` | Both | Path segment: full URL is `ws://HOST:PORT/ws/PATH`. |
| `UNIQUE_API_PORT` | `5005` | api_server.py only | Port for the Flask API (e.g. `/get-unique-id`). |

## Testing with Postman

**Base URL** (use your server IP if testing from another machine):

- Same machine: `http://localhost:5005` or `http://127.0.0.1:5005`
- From network: `http://10.0.0.109:5005`

**HTTP endpoints (GET, no body):**

| Purpose | URL | Method |
|--------|-----|--------|
| Get unique ID from WebSocket | `http://10.0.0.109:5005/get-unique-id` | GET |
| Generate unique ID locally (fallback) | `http://10.0.0.109:5005/generate-local-id` | GET |
| Health check | `http://10.0.0.109:5005/health` | GET |
| API info / docs | `http://10.0.0.109:5005/` | GET |

In Postman: New Request → set method to **GET** → paste the URL → Send. Replace `10.0.0.109` with `localhost` if you're on the same machine.

**WebSocket (optional):**  
`ws://10.0.0.109:7845/ws/voicebot_session_2002` — connect in Postman via New → WebSocket Request; the server sends a JSON with `unique_id` on connect.

## Mapping to a domain

**Option 1 – Same machine, different domain (Apache/Nginx)**  
Keep defaults and point your domain to this server; set only if needed:

- `WEBSOCKET_HOST` = your server’s IP or hostname (if clients use a domain to connect).
- `UNIQUE_API_PORT` = port the API runs on (e.g. 5005).

**Option 2 – Different host per domain**  
Run one set of processes per domain and use a different `.env` or systemd env:

Example for **catalyst.syntheon.in**:

```bash
export WEBSOCKET_HOST=10.0.0.109
export WEBSOCKET_PORT=7845
export WEBSOCKET_PATH=voicebot_session_2002
export UNIQUE_API_PORT=5005
python websocket_server.py &
python api_server.py
```

Example for another domain (different host/port):

```bash
export WEBSOCKET_HOST=10.0.0.110
export WEBSOCKET_PORT=7846
export WEBSOCKET_PATH=voicebot_session_2003
export UNIQUE_API_PORT=5006
python websocket_server.py &
python api_server.py
```

Then in Apache/Nginx, proxy each domain to the correct `UNIQUE_API_PORT` (and WSS to the matching `WEBSOCKET_HOST:WEBSOCKET_PORT` if needed).

## Systemd service files

Two service files are included:

- **unique-websocket.service** – runs `websocket_server.py`
- **unique-api.service** – runs `api_server.py`

**Install and run (run these on the server):**

```bash
# Copy service files (source first, then destination directory)
sudo cp /var/www/html/live_calls/unique/unique-websocket.service /etc/systemd/system/
sudo cp /var/www/html/live_calls/unique/unique-api.service /etc/systemd/system/

# Reload systemd and enable + start both services
sudo systemctl daemon-reload
sudo systemctl enable unique-websocket unique-api
sudo systemctl start unique-websocket unique-api

# Check status
sudo systemctl status unique-websocket unique-api
```

Each `cp` needs two arguments: **source file** and **destination** (here the directory `/etc/systemd/system/`).

**Useful commands:**

```bash
sudo systemctl stop unique-websocket unique-api   # stop both
sudo systemctl restart unique-websocket unique-api
sudo journalctl -u unique-websocket -f
sudo journalctl -u unique-api -f
```

**Troubleshooting**

- **`Active: activating (auto-restart)` / exit code 1**  
  Check the real error:
  ```bash
  sudo journalctl -u unique-websocket -u unique-api -n 30 --no-pager
  ```
- **"Address already in use" (port 7845 or 5005)**  
  Another process is using the port (e.g. manually started Python). Free the ports, then restart:
  ```bash
  sudo systemctl stop unique-websocket unique-api
  pkill -f websocket_server.py
  pkill -f api_server.py
  sudo systemctl start unique-websocket unique-api
  sudo systemctl status unique-websocket unique-api
  ```
- **`ModuleNotFoundError: No module named 'websockets'` (or `flask`)**  
  Services run as `www-data` but packages are in your user’s environment.
  The included `.service` files use `User=vmc` so they use your env; for production as `www-data`, run: `sudo pip3 install websockets flask`.

Services run as `vmc` by default so they see your Python packages; change `User`/`Group` in the `.service` files if needed.

## Files

- **websocket_server.py** – Serves unique IDs over WebSocket; reads `WEBSOCKET_HOST`, `WEBSOCKET_PORT`, `WEBSOCKET_PATH`.
- **api_server.py** – HTTP API that gets a unique ID from the WebSocket server; reads the same three vars plus `UNIQUE_API_PORT`.

Both must use the same `WEBSOCKET_*` values so the API can connect to the WebSocket server.
