Running Laravel Websockets in production

G
3 min readDec 12, 2020

--

Laravel WebSockets

Updates 9th Feb 2022:

Have recently come across Socketi which is an awesome alternative to pusher websockets, checkout the documentation here: https://docs.soketi.app/getting-started/client-configuration/laravel-echo
I highly recommend switching to this, as Laravel Websockets seems semi-abandoned.

Updates:

After a ton of questions, I’ve decided to rewrite the tutorial to cover the four major aspects of deploying Laravel websockets to production. My aim is to make it as easy as possible to understand. This tutorial will now be divided into a 4 part series as shown below.

  1. Initial configuration — Start here, then choose one of the deployment architectures below.
  2. Setting up websockets for a single application.
  3. Setting up websockets for multi-tenant application
  4. Setting up a dedicated websockets server.

Initial Configurations

Laravel websockets by beyondcode is a pretty good package for running your own pusher websockets server, I am going to show you how to run one in your own production server. This tutorial assumes that you are using a VPS and not a shared server, but if you find a way to execute the steps below from a shared server, then great. Secondly we are only focusing on running the websockets in production, I assume you are already familiar with setting up pusher, and Laravel-websockets in Laravel. If not, please check the links below.

  1. Self-hosting Laravel websockets server — YouTube
  2. Laravel's docs — Broadcasting

Requirements

  1. Laravel websockets — The installation guide can be found here
  2. Supervisor
  3. Reverse proxy server, such as nginx, (apache2 — mod_proxy)

Step 1 — Installing Laravel websockets

Please visit the guide above to install Laravel websockets.

Step 2— Installing supervisor

Supervisor is a process control system that allows the system to monitor applications and make sure they are always running. This will ensure that whenever the application terminates unexpectedly, it can always be restarted.

You can follow this installation guide to see how you can install one for your system. Here is a summary.

apt install supervisor

Once supervisor has been installed, we now need to configure it to run the websockets. To confirm if supervisor is installed, run the following on the terminal.

supervisorctl status

Navigate to the configuration cd /etc/supervisor/conf.d/

Let’s add the configurations for supervisor.

touch websockets.conf

Use any text editor to edit the websockets.conf file above, I prefer nano

nano websockets.conf

Then add the following configuration options

[program:laravel-websockets]
directory=/path/to/project/root/directory
command=php artisan websockets:serve --host 127.0.0.1 --port 6000
numprocs=1
user=[user] ;e.g. admin
autostart=true
autorestart=true
stderr_logfile=/var/log/websockets.err.log
stdout_logfile=/var/log/websockets.out.log

Please don't set the user as root for security concerns, instead use a user with limited privileges. Moreover, you can configure the port to be other than 6000. As you may have noticed, the host is 127.0.0.1 there is a good reason for this, as we’ll see later.

Lastly, to reload the supervisor configuration run.

service supervisor restart

If you encounter an error on this step, check your configuration for possible mistakes. Running journalctl -xe or service supervisor status should help in debugging.

Hooray !! If you have made it this far, then you’ve got a basic running websockets. From here we diverge on the specifics of implementation, kindly choose one of the architectures of deployment you might be interested in. Those tutorials are covered in separate articles.

  1. Setting up websockets for a single application. — You have one application and one tenant, i.e., the normal set-ups.
  2. Setting up websockets for multi-tenant application. — You have one application, multiple tenants.
  3. Setting up a dedicated websockets server. — A dedicated application/server for websockets with one or more clients connected.

Final thoughts

Hopefully one of the above deployment models works for you. Good luck. In case of any query, kindly reach out.

--

--

G

Backend developer with 7+ years of experience. Specializing mainly in PHP Laravel. Interested in natural simulations, visualization systems, and anything nerdy