How to run laravel websocket on heroku?

by hal.littel , in category: PHP Frameworks , 9 days ago

How to run laravel websocket on heroku?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by jasen , 9 days ago

@hal.littel 

To run a Laravel Websocket Server on Heroku, follow these steps:

  1. Create a new Laravel application by running the command:
1
composer create-project --prefer-dist laravel/laravel example-app


  1. Install the laravel-websocket package by running:
1
composer require beyondcode/laravel-websockets


  1. Publish the configuration file by running:
1
php artisan vendor:publish --provider="BeyondCodeLaravelWebSocketsWebSocketsServiceProvider" --tag="config"


  1. Add your Heroku app URL to the trusted_proxies array in the websockets.php configuration file:
1
2
3
4
5
'trusted_proxies' => [
    '192.168.1.1',
    '10.0.0.1',
    'proxy-url-on-heroku.com',
],


  1. Modify your broadcasting.php configuration file to use the pusher driver provided by the laravel-websockets package:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
'connections' => [
    'pusher' => [
        'driver' => 'pusher',
        'key' => env('PUSHER_APP_KEY'),
        'secret' => null,
        'app_id' => env('PUSHER_APP_ID'),
        'options' => [
            'cluster' => env('PUSHER_APP_CLUSTER'),
            'encrypted' => false,
            'host' => 'your-heroku-app-url',
            'port' => 6001,
            'scheme' => 'http',
        ],
    ],
],


  1. Create a Procfile in the root directory of your Laravel application with the following content:
1
web: bin/start-socket.sh


  1. Create a bin/start-socket.sh file with the following content:
1
php artisan websockets:serve


  1. Commit your changes to your git repository and then deploy your Laravel application to Heroku.
  2. Scale your worker dyno on Heroku by running the following command:
1
heroku ps:scale web=1:free worker=1:free


  1. Run the Websockets serve command on Heroku by running:
1
heroku run php artisan websockets:serve


Your Laravel Websocket server should now be up and running on Heroku. You can test the functionality by connecting to your websocket server from a client application.