# Fix WebSocket Connection Issue - catalyst.syntheon.in

## Problem
WebSocket connection to `wss://catalyst.syntheon.in/ws/...` is failing with:
- Error: `Unexpected server response: 200`
- Apache is returning HTML instead of upgrading to WebSocket
- Status: 200 OK with `Content-Type: text/html`

## Root Cause
The Apache configuration is missing:
1. WebSocket upgrade detection (RewriteCond for Upgrade header)
2. Proper ProxyPass configuration
3. HTTP proxy for non-WebSocket requests

## Solution

### Step 1: Update Apache Configuration

Replace the current configuration with the fixed version:

```bash
# Backup current config
sudo cp /etc/apache2/sites-available/catalyst.syntheon.in.conf /etc/apache2/sites-available/catalyst.syntheon.in.conf.backup

# Copy the fixed configuration
sudo cp /var/www/html/live_calls/catalyst.syntheon.in.conf.fixed /etc/apache2/sites-available/catalyst.syntheon.in.conf
```

### Step 2: Ensure Required Modules are Enabled

```bash
# Check if modules are enabled
sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod proxy_wstunnel
sudo a2enmod rewrite
sudo a2enmod ssl
sudo a2enmod headers
```

### Step 3: Test Configuration

```bash
sudo apache2ctl configtest
```

Should output: `Syntax OK`

### Step 4: Reload Apache

```bash
sudo systemctl reload apache2
```

### Step 5: Test WebSocket Connection

```bash
# Test with curl (should show upgrade)
curl -i -N \
  -H "Connection: Upgrade" \
  -H "Upgrade: websocket" \
  -H "Sec-WebSocket-Version: 13" \
  -H "Sec-WebSocket-Key: test" \
  https://catalyst.syntheon.in/ws/test
```

## Key Changes in Fixed Configuration

1. **Added RewriteEngine** - Detects WebSocket upgrade requests
2. **Added RewriteCond/RewriteRule** - Handles WebSocket upgrade headers
3. **Changed IP to localhost** - `ws://127.0.0.1:7900/ws/` instead of `ws://10.0.0.109:7900/ws/`
4. **Added HTTP ProxyPass** - For non-WebSocket requests
5. **Added SSL options** - Include Let's Encrypt SSL options

## Verify Service is Running

```bash
# Check if service is running on port 7900
sudo netstat -tlnp | grep 7900

# Check service status
sudo systemctl status websocket_api.service
```

## Test in Postman

1. Create new WebSocket request
2. URL: `wss://catalyst.syntheon.in/ws/testsession_123`
3. Should connect successfully (not return 200 HTML)

## Troubleshooting

### If still getting 200 HTML response:

1. **Check Apache error logs:**
   ```bash
   sudo tail -f /var/log/apache2/catalyst.syntheon.in-ssl-error.log
   ```

2. **Verify modules are loaded:**
   ```bash
   apache2ctl -M | grep -E "(proxy|rewrite|ws)"
   ```

3. **Check if service is accessible:**
   ```bash
   curl http://127.0.0.1:7900/
   # Should return: {"message": "Mcube Call Service Server is running!"}
   ```

4. **Test direct WebSocket connection:**
   ```bash
   wscat -c ws://127.0.0.1:7900/ws/test
   ```

## Current Configuration Issues

❌ Missing RewriteEngine for WebSocket upgrade detection
❌ Missing HTTP ProxyPass for regular requests  
❌ Using external IP instead of localhost
❌ Missing SSL options include

## After Fix

✅ WebSocket upgrade properly detected
✅ Requests proxied to localhost:7900
✅ Both HTTP and WebSocket work
✅ SSL properly configured

