Websockets

Setup beyondcode/laravel-websockets with Laravel forge

Max Hutschenreiter -

Laravel-websockets package by beyondcode brings the power of WebSockets to your Laravel application. It’s a great free pusher replacement and it offers SSL and Laravel Echo support.

It’s really easy to set it up and replace pusher on your local project.

When I tried to implement it into a live project driven by Laravel forge, it was not a perfectly easy cause of Laravel forge limitations so I will share a workaround on that.

Laravel forge doesn’t allow you to add multiple server blocks within one site so it’s not possible to add another server directive that runs on a different port (6001 by default), so we will create another site with another Laravel instance and proxy pass from 443 to port 6001 on that instance.

The first thing to do is to extend .env file on live project that will work as sockets client:
PUSHER_APP_ID=whatever
PUSHER_APP_KEY=whatever
PUSHER_APP_SECRET=whatever
PUSHER_APP_CLUSTER=whatever
PUSHER_APP_HOST=sockets.domain.com
PUSHER_APP_PORT=443
And then on your domain sockets.domain.com
PUSHER_APP_ID=whatever
PUSHER_APP_KEY=whatever
PUSHER_APP_SECRET=whatever
PUSHER_APP_CLUSTER=whatever
Then on the domain that acts as sockets server, update the Nginx location block to the following:
location / { 
    proxy_pass http://127.0.0.1:6001/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header X-Forwarded-For $remote_addr; 
}
The next step is to set up a fresh Laravel instance on the sockets server and integrate Laravel-websockets package

when that’s done, add the Daemon process that will ensure running sockets server 24/7:
php artisan websockets:serve --port=6001
Once you get to the point where your sockets.domain.com returns 404 (which is a good sign) , update your main project bootstrap.js file with
import Echo from 'laravel-echo'
window.Pusher = require('pusher-js'); 
window.Echo = new Echo({
    broadcaster: 'pusher',
    key: 'whatever',
    wsHost: 'sockets.domain.com',
    wsPort: 443,
    disableStats: true,
    scheme: 'https'
});
And in broadcasting.php
'cluster' => env('PUSHER_APP_CLUSTER'),
'host' => env('PUSHER_APP_HOST'),
'port' => env('PUSHER_APP_PORT'),
'encrypted' => true,
'scheme' => 'https'
So by the end of the process, you should have two separate sites. Both of them have beyondcode/laravel-websockets package integrated, but one acts as a client (main application) and one acts as a sockets server (sockets.domain.com).

Env pusher secret details need to be the same for both and that’s it.

Tags: Laravel · Websockets · Beyondcode · Forge

Want products news and updates?

Sign up for our newsletter to stay up to date.

We care about the protection of your data. Read our Privacy Policy.

Impressions from our Team

  • Happy birthday 🎁🎈🎂 Filip - #

  • Another day another #mandarinacakeshop 🎂 😀 - #

  • Happy Birthday Ognjen! And marry Christmas to all other 🎄#notacakeshop - #

  • #Office #Garden - #

  • #workhard - #

  • #belgrade #skyline - #

  • #happybirthday Phil :) - #

  • #happybirthday Stefan 🥂 - #

  • #happybirthday Lidija 🍾 - #

  • Say hi 👋 to our newest team member ☕️ - #

  • #bithday #cake 😻 - #

  • #stayathome #homeoffice #42coders - #

  • #stayathome #homeoffice #42coders #starwars :) - #

  • #stayathome #homeoffice #42coders - #

  • We had a really nice time with #laracononline #laravel - #

  • Happy Birthday 🎂 Miloš - #

  • Happy Birthday 🎂Nikola - #

  • #42coders #christmas #dinner what a nice evening :) - #

  • Happy Birthday 🎂 Ognjen - #

  • Wish you all a merry Christmas 🎄🎁 - #

See more!

© 2024 42coders All rights reserved.