### event作成
$ php artisan make:event Test
app/Events/Test.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 | '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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | Route::get( '/sent' , function (){ event( new \App\Events\Test( 'テストメッセージ' )); }); Route::get( 'receive' , function (){ return <<<HTML <!DOCTYPE html> <head> <title>Pusher Test</title> <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のままだと動かないので注意が必要です。