### event作成
$ php artisan make:event Test
app/Events/Test.php
class Test implements ShouldBroadcast { use Dispatchable, InteractsWithSockets, SerializesModels; /** * Create a new event instance. * * @return void */ public $message; public function __construct($message) { // $this->message = $message; } /** * Get the channels the event should broadcast on. * * @return \Illuminate\Broadcasting\Channel|array */ public function broadcastOn() { return new Channel('test_channel'); } public function broadcastAs() { return 'test_event'; } }
### config/broadcasting.php
'default' => env('BROADCAST_DRIVER', 'pusher'), 'connections' => [ '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, ], ],
### web.php
Route::get('/sent', function(){ event(new \App\Events\Test('テストメッセージ')); }); Route::get('receive', function(){ return <<<HTML <!DOCTYPE html> <head> <title>Pusher Test</title> <script src="https://js.pusher.com/3.2/pusher.min.js"></script> <script> Pusher.logToConsole = true; var pusher = new Pusher('******************',{ cluster : 'ap3', forceTLS: true }); var pusherChannel = pusher.subscribe('test_channel'); pusherChannel.bind('test_event', function(data){ alert(data.message); }); </script> </head> HTML; });
broadcastdriverが初期値のnullのままだと動かないので注意が必要です。