# WebSocket Proxy Configuration

Untuk terminal console WebSocket berfungsi dengan baik, reverse proxy (Nginx/Apache) perlu dikonfigurasi untuk mendukung WebSocket upgrade.

## Nginx Configuration

Tambahkan konfigurasi berikut di Nginx untuk mendukung WebSocket:

```nginx
server {
    listen 80;
    listen [::]:80;
    server_name atlas-demo.avt.data-center.id;

    # WebSocket upgrade headers
    map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }

    location /api/v1/system/terminal/ws {
        proxy_pass http://localhost:8080;
        proxy_http_version 1.1;
        
        # WebSocket headers
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        
        # WebSocket timeouts
        proxy_read_timeout 86400s;
        proxy_send_timeout 86400s;
    }

    location /api {
        proxy_pass http://localhost:8080;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
```

## Apache Configuration (mod_proxy_wstunnel)

Jika menggunakan Apache, pastikan mod_proxy_wstunnel diaktifkan:

```apache
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so

<VirtualHost *:80>
    ServerName atlas-demo.avt.data-center.id

    # WebSocket endpoint
    ProxyPass /api/v1/system/terminal/ws ws://localhost:8080/api/v1/system/terminal/ws
    ProxyPassReverse /api/v1/system/terminal/ws ws://localhost:8080/api/v1/system/terminal/ws

    # Regular API
    ProxyPass /api http://localhost:8080/api
    ProxyPassReverse /api http://localhost:8080/api

    # Frontend
    ProxyPass / http://localhost:3000/
    ProxyPassReverse / http://localhost:3000/
</VirtualHost>
```

## Testing WebSocket Connection

Setelah konfigurasi, test dengan:

```bash
# Test WebSocket connection
wscat -c wss://atlas-demo.avt.data-center.id/api/v1/system/terminal/ws?token=YOUR_TOKEN
```

atau menggunakan curl:

```bash
curl -i -N \
  -H "Connection: Upgrade" \
  -H "Upgrade: websocket" \
  -H "Sec-WebSocket-Version: 13" \
  -H "Sec-WebSocket-Key: test" \
  http://localhost:8080/api/v1/system/terminal/ws?token=YOUR_TOKEN
```

## Troubleshooting

1. **Error: WebSocket connection failed**
   - Pastikan reverse proxy dikonfigurasi dengan benar
   - Check log backend untuk error details
   - Pastikan port 8080 accessible

2. **Connection closed immediately**
   - Check WriteTimeout di server config (harus 0 untuk WebSocket)
   - Check proxy timeouts (harus cukup panjang)

3. **401 Unauthorized**
   - Pastikan token valid dan tidak expired
   - Check authentication middleware
