If you have ever deployed your code to a production environment and then you realized that for some reason Pusher does not work but it does 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.
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:
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.
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.
If you have followed the steps above, then your events should work fine now.
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.