Hi, before starting this tutorial, I recommend reading The Ultimate Guide to Laravel Reverb: Real-Time Notifications, as this tutorial assumes you already have Laravel Reverb set up and working locally.
Basic Concepts
First, it’s essential to understand how port connections work. Laravel Reverb uses two ports: one for connecting to the WebSocket and another for server communication.
In this case, we will use port 443 for public access. This port is crucial for users accessing the webpage as it handles secure HTTPS traffic.
For WebSocket communication, we will use port 6001. This port doesn’t need to be publicly accessible since it is only used internally by the server for real-time communication.
Configurations
Now, let's modify out .env
REVERB_HOST="your.domain.com" # your domain name here
REVERB_PORT=443 # The public port used by all clients on your website
REVERB_SCHEME=https # Required for prod environment
Them in your nginx configuration put the next code.
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name your.server.name;
{{ssl_certificate_key}}
{{ssl_certificate}}
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";
proxy_pass http://0.0.0.0:6001;
}
location /apps {
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:6001;
}
}
What we do here? This setup securely redirects all WebSocket traffic from the public-facing port 443 to the internal WebSocket server on port 6001, enabling secure and efficient real-time communication between clients and the server.
For Apache configurations you can read these posts.
- https://github.com/laravel/framework/discussions/50675
- https://github.com/laravel/reverb/issues/107#issuecomment-2019340122
After completing the configuration, the next step is to start the service. In a real-world scenario, you should use a process manager, such as Supervisor, to ensure the service runs continuously and restarts automatically if needed.
For testing purposes, however, you can run the command manually from the command line.
php artisan reverb:start --port=6001 --debug
Now we are ready to work with WebSocket in a production environment. Remember to restart the service and clear the cache before testing to ensure everything runs smoothly.
A post that can help us gain a deeper understanding of this topic.
- https://laravel.com/docs/11.x/reverb#production
- https://medium.com/@saddanfah/how-to-use-laravel-reverb-in-production-server-d89e9670b76a
I hope this helps you get started! See you in the next post.
Top comments (0)