shoaiblinker's avatar

Laravel Reverb websocket connection connect and disconnect immediately

I am unable to make websocket connection on VPS server. Although, everything is working perfect in local machine. The nginx server is running properly on VPS . The connection of sub-domain is SSL secure and working. It also proxy_pass to localhost 8080 but connection to websocket couldn't be established permanently.

Here is the Laravel Reverb Log Connection Established ........................................................ 976134326.890116133
Connection Closed ............................................................... 976134326.890116133

The connection hitting the Reverb server . Nginx Access log

[27/Nov/2024:18:02:48 +0500] "GET /app/xxxxx{app_key}?protocol=7&client=js&version=8.3.0&flash=false HTTP/1.1" 101 116 "-" "Mozilla/5.0

101 Switching Protocol

Client Header Request sec-websocket-key:Y97pzDpX64OFaw6ivCg/uQ== sec-websocket-version:13 upgrade: websocket

Nginx Config - Use exactly from Laravel Documentation

location / {
    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";

    proxy_pass http://0.0.0.0:8080;
}

In .env REVERB_APP_ID=5428334 REVERB_APP_KEY=vf1sychhnzbohhsja9r REVERB_APP_SECRET=my-app-secret REVERB_HOST="localhost" REVERB_PORT=8080 REVERB_SCHEME=http

What I'm doing wrong

0 likes
3 replies
LaryAI's avatar
Level 58

To troubleshoot the issue with your WebSocket connection on a VPS server using Laravel Reverb, let's go through a few potential solutions and checks:

  1. Check WebSocket Server Binding: Ensure that your WebSocket server is correctly binding to the public IP address or 0.0.0.0 instead of localhost. This allows it to accept connections from external clients.

    REVERB_HOST="0.0.0.0"
    
  2. Firewall and Security Groups: Verify that your VPS firewall or any security groups (if using a cloud provider like AWS) allow traffic on port 8080. You might need to open this port for both inbound and outbound traffic.

  3. Nginx Configuration: Your Nginx configuration seems mostly correct, but ensure that the proxy_pass directive is pointing to the correct address and port. Also, make sure that the proxy_set_header Connection "Upgrade"; is correctly set to handle WebSocket connections.

  4. SSL Configuration: If your sub-domain is SSL-secured, ensure that your WebSocket server is also configured to handle SSL connections. You might need to use wss:// instead of ws:// in your client-side code if SSL is required.

  5. Check Logs: Look at both the Nginx error logs and the Laravel Reverb logs for any additional error messages that might provide more context on why the connection is closing.

  6. Client-Side Code: Ensure that your client-side code is correctly attempting to connect to the WebSocket server using the correct protocol (ws:// or wss://), host, and port.

  7. Reverb Configuration: Double-check your .env configuration for any typos or incorrect values. Ensure that the REVERB_HOST and REVERB_PORT match the server's configuration.

  8. Testing Locally: If possible, try running the WebSocket server locally on the VPS and connect using a tool like wscat to ensure that the server is accepting connections.

By following these steps, you should be able to identify and resolve the issue with your WebSocket connection on the VPS server. If the problem persists, consider providing more detailed logs or error messages for further assistance.

shoaiblinker's avatar
shoaiblinker
OP
Best Answer
Level 4

After reading every line of nginx conf. I found there is one directive proxy_hide_header Upgrade. Remove this! šŸ¤. It works!

For future reference for anyone!

1 like

Please or to participate in this conversation.