Running Laravel Websockets in production — Setting up websockets for a single application

G
3 min readSep 7, 2021

--

Setting up websockets for a single application

This is a continuation of the ‘Running Laravel Websockets in production’ tutorial series. This tutorial is part of a 4 part series, as shown below.

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

If you haven’t looked at the previous tutorial which includes setting up the initial configurations i.e., the web sockets server itself and supervisor kindly read it here.

Introduction

This deployment model is the most common and basically what we are interested in is having one web sockets server, serving just one application.

Single application approach

In this approach, we will be connecting the client browser using echo JS specifically pusher to our websockets running on https://example.com.

In the above instance, both the Laravel application and websockets application will be running on the same domain. Replace the example.com with your own domain/IP.

Pros

  • Easier to debug.
  • Simpler configurations required.
  • Easier to set up.

Cons

  • Serves only one application.

Step 1 — Customizing supervisor

This has already been mentioned in the first article. I’ll just put the config files for reference purpose.

[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

After setting this configuration you can simply start supervisor by running service supervisor restart or if the service is not running then simply run service supervisor start

To check if your websockets is running, then use the command supervisorctl statusor service supervisor status

Please note we are running the websockets from localhost, this is by design in that we will proxy the requests from clients to the websockets running on localhost.

Step 2 — Setting up reverse proxy

This is the most important step, and you should read this carefully.

We will be setting up reverse proxies for apache2 and nginx.

As a general tip, we are simply proxying the request to our websockets server being served on http://127.0.0.1:6000 as defined on the supervisor config above.

Nginx

The following will be the configuration for nginx. Add the following configuration to your existing Laravel app nginx configuration.

...location /app/ {
proxy_pass http://127.0.0.1:6000/app/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
}
location /apps/ {
proxy_pass http:127.0.0.1:6000/apps/;
proxy_set_header Host $host;
}...

For example, the configuration below is for our site https://example.com

Nginx conf

Apache2

For apache make sure that the apache mod_proxy is enabled, a tutorial on how to do this can be found here. Add the following configuration to your existing web application configuration.

...ProxyPass "/app/" "ws://127.0.0.1:6000/app/"
ProxyPass "/apps/" "http://127.0.0.1:6000/apps/"
...

For our example website, the configuration will be.

General warnings and tips

As a general tip, your app should avoid using the app/ and the apps/ route to avoid conflicts with the websockets server.

Step 3 — Laravel config files.

This shows the configurations for the Laravel web project.

websockets.conf

There will be no change to the default websockets.conf file

broadcasting.conf

echo.js

A dedicated script file for echo js

Conclusion

You should have a running web socket application by now. If you are interested in another deployment model, choose from one below.

  1. Setting up websockets for multi-tenant application. — You have one application, multiple tenants.
  2. Setting up a dedicated websockets server. — A dedicated application/server for websockets with one or more clients connected.

--

--

G

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