# Laravel reverb Config

After setting up reverb to be running on your vps, there are few changes you need to make all your server route websocket request to your laravel reverb.

### 🔧 NGINX Configuration

To proxy WebSocket requests to Laravel Reverb via Nginx, you must edit your virtual host (`vhost`) file or Nginx site configuration.

Add the following **`location`** block inside your server block:

```
location /app {
    proxy_http_version 1.1;
    proxy_set_header Host $http_host;
    proxy_set_header Scheme $scheme;
    proxy_set_header SERVER_PORT $server_port;
    proxy_set_header REMOTE_ADDR $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    
    # CORS headers for WebSocket
    add_header Access-Control-Allow-Origin *; # Or specify your domain
    add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
    add_header Access-Control-Allow-Headers 'Origin, Content-Type, Accept, X-Requested-With';
    
    proxy_pass http://127.0.0.1:6001;
}
```

**⚠️ Important**

Make sure the IP address and port in `proxy_pass` match the **Reverb server port** defined in your `.env` file:

```
REVERB_SERVER_PORT=6001
```

If you change these values in `.env`, you **must** also update the Nginx configuration to reflect the new host and port.

After editing the config:

```
sudo nginx -t     # Test the config
sudo systemctl reload nginx
```

#### 🔧 Apache Configuration

If you are using **Apache**, ensure the following is added to your virtual host configuration:

```
<VirtualHost *:80>
    ServerName yourdomain.com

    # Usual Laravel setup...
    DocumentRoot /var/www/yourproject/public

    <Directory /var/www/yourproject/public>
        Options Indexes FollowSyLinks
        AllowOverride All
        Require all granted
    </Directory>

    # WebSocket proxy configuration
    ProxyPass "/app" "http://127.0.0.1:6001/app"
    ProxyPassReverse "/app" "http://127.0.0.1:6001/app"

    RewriteEngine On
    RewriteCond %{HTTP:Upgrade} =websocket [NC]
    RewriteRule /app/(.*)           ws://127.0.0.1:6001/app/$1 [P,L]
    RewriteCond %{HTTP:Upgrade} !=websocket [NC]
    RewriteRule /app/(.*)           http://127.0.0.1:6001/app/$1 [P,L]

    # Optional CORS headers
    Header always set Access-Control-Allow-Origin "*"
    Header always set Access-Control-Allow-Methods "GET, POST, OPTIONS"
    Header always set Access-Control-Allow-Headers "Origin, Content-Type, Accept, X-Requested-With"
</VirtualHost>

```

**⚠️ Important**

As with Nginx, you **must ensure** that the port (`6001`) align with:

```
REVERB_SERVER_PORT=6001
```

Apply changes and restart Apache:

```
sudo a2enmod proxy proxy_http proxy_wstunnel headers rewrite
sudo systemctl restart apache2
```

### .ENV Configuration

Here is the expected reverb configuration in your .env. If file differs, please update your .env with values in the below code:

```
REVERB_APP_ID=301558 #Your preferred ID
REVERB_APP_KEY=c0s0RweKnOag #Your preferred Key
REVERB_APP_SECRET=7C589VDbxUhbMSoqx1 #Your preferred Secret
REVERB_SERVER_HOST=0.0.0.0 #Must be same as this value
REVERB_SERVER_PORT=6001 #Must be same as this value
REVERB_HOST=instalhaul.test #Change to your backend domain
REVERB_PORT=443 #Must be same as this value
REVERB_SCHEME=https #Must be same as this value
```
