If you have ever deployed your laravel code to production environment and then you realized that for some reasons Pusher does not work in your production environment , then you are not alone. I will show you how to make Pusher work in your production environment from the server side.
To make Pusher work in production, we need to ensure that Pusher has been set up correctly in our application. Let's set up Pusher on our existing Laravel application. If you have already installed Pusher, then skip to the next step.
If you don't know how to create a fresh Laravel application click here
Step 1: Install Pusher
composer require pusher/pusher-php-server
Once installation is complete, we'll make sure that Laravel broadcasting is registered in config/app.php. Go to config/app.php and uncomment the line below in the providers array.
// App\Providers\BroadcastServiceProvider
If you need to know more about creating and defining events then take a look at this
Step 2: Configure Pusher
We will configure our pusher app credentials in config/broadcasting.php. We will create and get our Pusher credentials from here. And now. we'll supply our credentials in the .env file as thus:
PUSHER_APP_ID=xxxxxx
PUSHER_APP_KEY=xxxxxxxxxxxxxxxxxxxx
PUSHER_APP_SECRET=xxxxxxxxxxxxxxxxxxxx
PUSHER_APP_CLUSTER=xx
Replace the xs with the actual pusher credentials
Now, we need to modify the pusher array in config/broadcasting.php so that it looks like this:
'pusher' => [
'driver' => 'pusher',
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'app_id' => env('PUSHER_APP_ID'),
'options' => [
'cluster' => env('PUSHER_APP_CLUSTER'),
'useTLS' => true,
'encrypted' => true,
],
],
Step 3: Update .env and Run Artisan Command
We will now change the value of our broadcast driver in the .env from log to pusher. This tells Laravel what broadcast driver to use when implementing ShouldBroadcast interface in our event class.
BROADCAST_DRIVER=pusher
And finally, run this command in your production environment
php artisan config:clear
You might want to run the following command;
php artisan tinker
and then check that your broadcasting configuration is correct - particularly the default connection which should be pusher
- with the following line of code.
config('broadcasting');
If you have followed the steps above, then your events should work fine now.
Conclusion
You have learned how to make Pusher work in a production environment.
Please note: there could be a number of reasons why your Pusher events do not work in your production environment. This is just one of them.
Top comments (0)