1. Register pusher
1. Register
https:// pusher.com/
2. Get key, secret key, app_id, etc.
2. Configure pusher
1. Install pusher
composer require pusher/pusher-php-server
2. Configure config/broadcasting.php
'default' => env('BROADCAST_DRIVER', 'pusher'), .... 'pusher' => [ 'driver' => 'pusher', 'key' => env('PUSHER_KEY'), 'secret' => env('PUSHER_SECRET'), 'app_id' => env('PUSHER_APP_ID'), 'options' => [ 'cluster' => 'ap1', 'encrypted' => true ], ], .....
3. Create events
1. The code is as follows:
info = $info; } /** * 指定广播频道(对应前端的频道) * Get the channels the event should be broadcast on. * * @return array */ public function broadcastOn() { return ['my-channel']; } /** * 指定广播事件(对应前端的事件) * @return string */ public function broadcastAs() { return 'my-event'; } /** * 获取广播数据,默认是广播的public属性的数据 */ public function broadcastWith() { return ['info' => $this->info]; } }
2. Broadcast events do not require a listener; broadcast events need to inherit the interface ShouldBroadcast
4. Broadcast
1 .Trigger event
event(new \App\Events\PusherEvent('测试'));
2. Front-end code
ps:
1.pusher uses curl tohttps://pusher. com
Submit data, so you need to configure the certificate; otherwise the submission will fail
2. If you do not configure the certificate, you need to set curl’sCURLOPT_SSL_VERIFYPEER
andCURLOPT_SSL_VERIFYHOST
#curl_setopt($ch, CURLOPT_POSTFIELDS, $post_value of trigger invender/pusher/pusher-php-server/lib/Pusher.php
);
Added below:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
laravel tutorial
column!The above is the detailed content of Detailed explanation of how Laravel uses pusher to push messages. For more information, please follow other related articles on the PHP Chinese website!