Home >PHP Framework >Laravel >Detailed explanation of how Laravel uses pusher to push messages
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:
<?php namespace App\Events; use App\Events\Event; use Illuminate\Queue\SerializesModels; use Illuminate\Contracts\Broadcasting\ShouldBroadcast; class PusherEvent extends Event implements ShouldBroadcast { use SerializesModels; public $info; /** * PusherEvent constructor. */ public function __construct($info) { $this->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
<!DOCTYPE html> <head> <title>Pusher Test</title> <script src="https://js.pusher.com/4.0/pusher.min.js"></script> <script> // Enable pusher logging - don't include this in production Pusher.logToConsole = true; var pusher = new Pusher('XXX', { cluster: 'ap1', encrypted: true }); var channel = pusher.subscribe('my-channel'); channel.bind('my-event', function(data) { alert(data.info); }); </script> </head>
ps:
1.pusher uses curl to https://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’s CURLOPT_SSL_VERIFYPEER
and CURLOPT_SSL_VERIFYHOST
#curl_setopt($ch, CURLOPT_POSTFIELDS, $post_value of trigger in vender/pusher/pusher-php-server/lib/Pusher.php
);
Added below:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);For more technical articles related to laravel framework, please visit the
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!