Self-Host Your Own Websockets with Laravel - It's This Easy and Fast

Self-Host Your Own Websockets with Laravel – It's This Easy and Fast



Have you ever wanted to use Websockets without using an external service like Pusher? In this video I will show you how you finally can leverage the power of websockets in Laravel without any external services. We will use Laravel Echo to work with the sockets on the frontend and Laravel events to trigger events on the server side.

The method we will use to operate our Websockets today is a package called laravel-websockets. This package actually imports an instance of Ratchet, a low-level web sockets server, into our Laravel project to allow us to host a websocket server as part of an existing Laravel application. This is convenient because everything is self-contained in one Laravel application.

You can run this web socket server inside the Laravel app that requires web sockets, or you can choose to host a standalone application that runs this package for your own self-hosted websocket platform. It can support multiple clients just like other popular websocket services.

This package scales well, with over 15,000 concurrent connections maintained using just a simple 512Mb ram VPS server that usually costs around $5 a month.

▼ ▼ ▼ LINKS ▼ ▼ ▼

Next (Optional): Configuring Laravel-Websockets – Understanding the settings:

Learn Websockets in Detail with My Full (FREE) Course on YouTube:

Laravel-Websockets Package:

Laravel-Websockets Documentation:

Installation Snippets:

Does This Package Scale?:

▼ ▼ ▼ CODE SNIPPETS ▼ ▼ ▼

This was a straightforward setup tutorial, so no GitHub repo is provided.

To install in an existing Laravel app
1) Install via composer:
composer require beyondcode/laravel-websockets

2) Publish migration files:
php artisan vendor:publish —provider=“BeyondCodeLaravelWebSocketsWebSocketsServiceProvider” —tag=“migrations”

3) Publish Configuration Files:
php artisan vendor:publish —provider=“BeyondCodeLaravelWebSocketsWebSocketsServiceProvider” —tag=“config”

4) Run Migrations:
php artisan migrate

To start your web socket server run:
php artisan websockets:serve

==== FOLLOW ME ====

Subscribe for New Releases!

Subscribe to DevMarketer Insider

Twitter –
(ask me questions!)

==== QUESTIONS? ====

Leave a comment below and I or someone else can help you.
For quick questions you may also want to ask me on Twitter, I respond almost immediately.

Email me [email protected]

Thanks for all your support!

38 thoughts on “Self-Host Your Own Websockets with Laravel – It's This Easy and Fast

  1. How to access websocket server in pure js code? Like we use "var socket = new WebSocket('ws://ip:port/socket.php')"?

    I want realtime content of a file(hosted on server and changing) to my web browser?

    Any one help….

  2. Bloody hell; all I wanted was a way to refresh a status page when a database table was modified, on a very small app I'm building that will not have internet access. Might stick with my existing "<meta http-equiv="refresh" content="10"> solution to refresh the page every 10 seconds. Very primitive, but for one or two users, it will work (with a delay). I'm very new to Laravel, but simple solutions to simple tasks doesn't seem to be a feature. The assumption seems to be everyone wants to build a blog!!

  3. If you watched this video and helped you a lot, please subscribe 😀 a simple way to thank him for making our lives easier

  4. 17:47 Just a heads up: if you want to keep everything defined in your .env file, you can assign

    'host' => preg_replace('(:d+)', '', env('APP_URL'))

    inside the pusher options.

    Thank you so much for this video. Perfect topic. You have no idea how much I yearned for this.

  5. Dude thank you so freaking much for clearing up all these things for us. The previous series gave me complete understanding of the process and this video just blew my mind because of how easy it is made for us. Setting up web sockets in just few lines of code and learning how to do it for free in YouTube?! What an amazing time we live in guys, we should appreciate that. Thank you again!

  6. Hi… when I click "connect" I get "Channels current state is unavailable". Anyone else with the same problem??? How to solve this???

  7. very good tutor but i have the Uncaught ReferenceError: require is not defined(anonymous function) @ app.js. Have an idea??

  8. Very nicely presented and informative. 

    One thing I would suggest, instead of explaining how people should use more secure keys and secrets in their ENV for production, why not show them how to do it? It would probably have taken the same time. E.g. fire up php artisan tinker and enter "IlluminateSupportStr::random(64)"

  9. Thank you very much for this tutorial, you really explain well. Please I have an issue. Everything works fine for me, but the real time statistics doesn't show up for me.. it is just blank. Please is there a reason why???

  10. I love how you explain everything, is not a "to do" list, nice videos.
    Can you answer a question for me? Whats the difference between websockets and socket.io. I have just done a fast a simple live chat with nodejs and socket.io, why is so much more complicated to do it on laravel with websockets?

  11. Hello, i can configure WebSockets without Vue ? I have a Laravel 5.8 with Boostrap 4 Html template. I no use Vue. Thanks 🙂

  12. Getting this

    WebSocket connection to 'wss://127.0.0.1/app/myKey?protocol=7&client=js&version=7.0.0&flash=false' failed: Error in connection establishment: net::ERR_CERT_AUTHORITY_INVALID

    please help me out. Thanks

  13. i have a problem, websockets works without errors, I use the livettt.test domain, but websockets does not react to event in laravel, nothing happens

  14. Hey! Amazing video as usual. Would you be able to cover laravel deployment to maybe AWS or GC especially when using queues, storage, websockets, etc. Thanks

  15. i have an issue please anyhelp : in my chrome it shows me in the console this error : WebSocket connection to 'wss://localhost/app/2222?protocol=7&client=js&version=6.0.3&flash=false' failed: Error in connection establishment: net::ERR_CERT_AUTHORITY_INVALID . help me guys I wasted over 4 days trying to see where the problem is

  16. Lol, Actually I make it by my own without laravel nor ratchet, Just a simple php websocket server, But the problem is Pushed send header without encrypted key, That the only problem I had, Even though I tried old Version of Pusher.js Still the same, Yikes, I just Gave up

  17. please guide me live server @livewirescript tag not working vendor path not found because of vendor path in public folder

Leave a Reply

Your email address will not be published. Required fields are marked *