Laravel Reverb in Production Environment

devjm

Jenry Mazariegos

Posted on November 20, 2024

Laravel Reverb in Production Environment

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

Enter fullscreen mode Exit fullscreen mode

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;
}
}
Enter fullscreen mode Exit fullscreen mode

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.

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
Enter fullscreen mode Exit fullscreen mode

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.

I hope this helps you get started! See you in the next post.

💖 💪 🙅 🚩
devjm
Jenry Mazariegos

Posted on November 20, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Laravel Reverb in Production Environment
tutorial Laravel Reverb in Production Environment

November 20, 2024