bhosted's avatar

Laravel Reverb problem on first connection after restart

Hi,

For some reason I have to do a request on https://ws.example.com:6001, before being allowed to connect on the websocket from javascript. Is this a known issue? Or is there some setting I'm missing?

Marc

0 likes
3 replies
LaryAI's avatar
Level 58

It sounds like the issue you're encountering might be related to the initialization of the WebSocket server, particularly if you're using Laravel Websockets or a similar package. This could be due to the server not being fully ready to accept WebSocket connections immediately after a restart until it handles its first HTTP request.

Here are a few steps you can take to troubleshoot and potentially resolve this issue:

  1. Ensure WebSocket Service is Ready: Make sure that your WebSocket service is fully up and running before it starts accepting connections. You might need to add some logging to your WebSocket server to check if it's fully initialized at the time the first connection attempt is made.

  2. Check Proxy Settings: If you are running the WebSocket server behind a proxy (like Nginx), ensure that the proxy is correctly configured to handle WebSocket connections. Here’s a basic example of what the Nginx configuration might look like:

map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}

server {
    listen 443 ssl;
    server_name ws.example.com;

    ssl_certificate /path/to/ssl/cert.pem;
    ssl_certificate_key /path/to/ssl/key.pem;

    location / {
        proxy_pass http://127.0.0.1:6001;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_set_header Host $host;
    }
}
  1. Laravel Websockets Package Configuration: If you are using the Laravel Websockets package, check the websockets.php config file to ensure all settings are correct and optimized for your use case. Pay special attention to host, port, and path settings.

  2. Server Warm-Up: As a workaround, you might consider creating a warm-up script that sends an HTTP request to the WebSocket server right after it restarts. This can be a simple CURL command that you run as part of your deployment process:

curl https://ws.example.com:6001
  1. Check for Errors: Look at the server logs for any errors or warnings that might give you more insight into what happens when the first connection attempt fails.

  2. Client-Side Reconnection Logic: On the client side, implement reconnection logic that attempts to reconnect a few times if the first connection fails. This can be particularly useful if the WebSocket server takes a few moments longer to be ready for connections.

Here’s a simple example using JavaScript:

function connectWebSocket() {
    var ws = new WebSocket('wss://ws.example.com:6001');

    ws.onopen = function() {
        console.log('Connected');
    };

    ws.onerror = function() {
        console.log('Connection Error, retrying...');
        setTimeout(connectWebSocket, 5000); // Retry after 5 seconds
    };
}

connectWebSocket();

By implementing these steps, you should be able to better diagnose and potentially fix the issue with WebSocket connections immediately after a server restart.

bhosted's avatar

Did some more testing and it seems that when a browser connects for the first time, the connection fails. As soon as the connection is opened by the client using a https://ws.example.com:6001 request, the connection is up and running without any problems. Logout and login does not require a https://ws.example.com:6001 request to successfully create a connection.

bhosted's avatar

Still having issues, but it seems to be related to Chrome. Firefox and Safari are both working just fine. Can it be CORS related? I read that websockets are not checked for CORS?

Please or to participate in this conversation.