Mohamed Athif Mohamed Athif - 23 days ago 13
PHP Question

Pusher receives no events

i am using laravel with pusher to send an event message to pusher. the code is in my controller which is a post controller, triggered when an input form is submitted. below is my code. what am i doing wrong? there is no event received.
this is an ajax call route based controller.

$pusher = new Pusher( env('PUSHER_KEY'), env('PUSHER_SECRET'), env('PUSHER_APP_ID'), array( 'encrypted' => true ) );
$pusher->trigger( 'test_channel', 'my_event', 'hello world' );

Answer

I am also assuming you have set up your Pusher account correctly and that your environment variables are correct.

If so, you may need to ensure you are using the correct Cluster (the default is fine for the US, but outside the East coast of the US for example, the cluster must be explicitly defined).

Update:

Controller code:

<?php

namespace App\Http\Controllers;

use Vinkla\Pusher\Facades\Pusher;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

class TestPusherController extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;


    public function test(){
        $arr = array('test' => 'hello world 2') ;
        $pusher = new Pusher( env('PUSHER_KEY'), env('PUSHER_SECRET'), env('PUSHER_APP_ID'), array( 'encrypted' => true, 'cluster' => 'ap1' ) );
        $pusher::trigger( 'test_channel', 'my_event', $arr);

        return $arr;
    }

    public function shortenedTest(){
        $message = 'Hello world';
        Pusher::trigger('my-channel', 'my-event', ['message' => $message]);
    }

}

In web routes:

Route::get('testPusherController', 'TestPusherController@test');
Route::get('shortenedTestPusherController', 'TestPusherController@shortenedTest');

I have got this working on a fresh install of vinkla/pusher following the setup steps in https://github.com/vinkla/laravel-pusher, on Laravel 5.3, using the built in PHP server and Connecting to the EU server (I do not have any Pusher apps using ap1 at this time).

You will notice a small number of changes to the coding in the controller to get the correct format. You must 'use' the Pusher facade above the controller.

For completeness, I have added a neater way of working with this where you can set the Pusher credentials in the Config/pusher.php file without the need to setup the connection for each use. This can be seen in the shortenedTest() method on the controller.

<?php

return [

    'connections' => [
        'main' => [
            'auth_key' => env('PUSHER_KEY'),
            'secret' => env('PUSHER_SECRET'),
            'app_id' => env('PUSHER_APP_ID'),
            'options' => [
                'cluster' => env('PUSHER_CLUSTER')
            ],
            'host' => null,
            'port' => null,
            'timeout' => null,
        ],

        'alternative' => [
            'auth_key' => 'your-auth-key',
            'secret' => 'your-secret',
            'app_id' => 'your-app-id',
            'options' => [],
            'host' => null,
            'port' => null,
            'timeout' => null,
        ],

    ],

];
Comments