# Setup Guide: catalyst.syntheon.in with WebSocket Support

## Overview
This guide will help you configure `catalyst.syntheon.in` to route to your FastAPI live calls service with WebSocket support.

## Prerequisites
✅ Apache modules already enabled:
- `proxy` - HTTP proxy support
- `proxy_http` - HTTP proxy module
- `proxy_wstunnel` - WebSocket tunnel support
- `rewrite` - URL rewriting

## Step 1: Copy Configuration File

```bash
sudo cp /var/www/html/live_calls/catalyst.syntheon.in.conf /etc/apache2/sites-available/catalyst.syntheon.in.conf
```

## Step 2: Enable the Site

```bash
sudo a2ensite catalyst.syntheon.in.conf
```

## Step 3: Test Apache Configuration

```bash
sudo apache2ctl configtest
```

If you see "Syntax OK", proceed to the next step.

## Step 4: Reload Apache

```bash
sudo systemctl reload apache2
```

## Step 5: Verify the Service is Running

Make sure your FastAPI service is running on port 7900:

```bash
# Check if the service is running
ps aux | grep -E "(uvicorn|main.py)" | grep -v grep

# Or check if port 7900 is listening
sudo netstat -tlnp | grep 7900
# or
sudo ss -tlnp | grep 7900
```

If not running, start it:

```bash
cd /var/www/html/live_calls/homebook
python main.py
# or
uvicorn main:app --host 127.0.0.1 --port 7900
```

## Step 6: Test HTTP Access

```bash
# Test HTTP endpoint
curl http://catalyst.syntheon.in/
# Should return: {"message": "Mcube Call Service Server is running!"}

# Test health endpoint
curl http://catalyst.syntheon.in/health
```

## Step 7: Setup SSL (Optional but Recommended)

For HTTPS/WSS support, get an SSL certificate:

```bash
sudo certbot --apache -d catalyst.syntheon.in -d www.catalyst.syntheon.in
```

This will:
- Automatically configure SSL
- Update the configuration file with certificate paths
- Set up automatic renewal

## Step 8: Test WebSocket Connection

### Using wscat (if installed):
```bash
wscat -c ws://catalyst.syntheon.in/ws/testsession_123
# or for WSS (if SSL is configured):
wscat -c wss://catalyst.syntheon.in/ws/testsession_123
```

### Using JavaScript:
```javascript
const ws = new WebSocket('ws://catalyst.syntheon.in/ws/testsession_123');
// or for WSS:
// const ws = new WebSocket('wss://catalyst.syntheon.in/ws/testsession_123');

ws.onopen = () => {
    console.log('WebSocket connected');
};

ws.onmessage = (event) => {
    console.log('Message received:', event.data);
};

ws.onerror = (error) => {
    console.error('WebSocket error:', error);
};
```

## Configuration Details

### HTTP/HTTPS Routing:
- All HTTP requests to `catalyst.syntheon.in/` → `http://127.0.0.1:7900/`
- All HTTPS requests to `catalyst.syntheon.in/` → `http://127.0.0.1:7900/` (proxied internally)

### WebSocket Routing:
- `ws://catalyst.syntheon.in/ws/{session_id}` → `ws://127.0.0.1:7900/ws/{session_id}`
- `wss://catalyst.syntheon.in/ws/{session_id}` → `ws://127.0.0.1:7900/ws/{session_id}` (WSS to WS)

### Available Endpoints:
- `GET /` - Service status
- `GET /health` - Health check
- `GET /health/performance` - Performance metrics
- `WebSocket /ws/{session_id}` - Mcube call WebSocket connection

## Troubleshooting

### Issue: 502 Bad Gateway
**Solution**: Make sure FastAPI service is running on port 7900
```bash
cd /var/www/html/live_calls/homebook
python main.py
```

### Issue: WebSocket connection fails
**Solution**: Check Apache error logs
```bash
sudo tail -f /var/log/apache2/catalyst.syntheon.in-error.log
```

### Issue: SSL certificate errors
**Solution**: Run certbot to get SSL certificate
```bash
sudo certbot --apache -d catalyst.syntheon.in
```

### Issue: Apache configuration errors
**Solution**: Test configuration
```bash
sudo apache2ctl configtest
```

## Logs Location

- **HTTP Error Log**: `/var/log/apache2/catalyst.syntheon.in-error.log`
- **HTTP Access Log**: `/var/log/apache2/catalyst.syntheon.in-access.log`
- **HTTPS Error Log**: `/var/log/apache2/catalyst.syntheon.in-ssl-error.log`
- **HTTPS Access Log**: `/var/log/apache2/catalyst.syntheon.in-ssl-access.log`
- **FastAPI Logs**: `/var/www/html/live_calls/homebook/logs/voicebot.log`

## Update Your Application Configuration

After setting up the domain, you can update your application to use the domain-based WebSocket URL:

### In your `.env` file or environment:
```bash
# Old (IP-based):
MCUBE_WEBSOCKET_URL=ws://10.0.0.109:7900/ws/testsession_123
MCUBE_WEBSOCKET_BASE_URL=ws://10.0.0.109:7900/ws/

# New (domain-based):
MCUBE_WEBSOCKET_URL=ws://catalyst.syntheon.in/ws/testsession_123
MCUBE_WEBSOCKET_BASE_URL=ws://catalyst.syntheon.in/ws/

# Or with SSL (recommended):
MCUBE_WEBSOCKET_URL=wss://catalyst.syntheon.in/ws/testsession_123
MCUBE_WEBSOCKET_BASE_URL=wss://catalyst.syntheon.in/ws/
```

## Security Considerations

1. **Firewall**: Make sure port 80 and 443 are open
2. **SSL**: Use HTTPS/WSS in production (certbot will set this up)
3. **CORS**: Your FastAPI app already has CORS configured for all origins
4. **Rate Limiting**: Consider adding rate limiting for production use

## Next Steps

1. ✅ Copy configuration file
2. ✅ Enable site
3. ✅ Test configuration
4. ✅ Reload Apache
5. ✅ Verify service is running
6. ✅ Test HTTP endpoints
7. ✅ Setup SSL (optional)
8. ✅ Test WebSocket connection
9. ✅ Update application configuration to use domain-based URLs

## Summary

After completing these steps:
- ✅ `http://catalyst.syntheon.in/` will route to your FastAPI service
- ✅ `ws://catalyst.syntheon.in/ws/{session_id}` will work for WebSocket connections
- ✅ `https://catalyst.syntheon.in/` will work after SSL setup
- ✅ `wss://catalyst.syntheon.in/ws/{session_id}` will work after SSL setup

Your live calls service will be accessible via domain name instead of IP address!

