Home> PHP Framework> Laravel> body text

Detailed explanation of how Laravel uses pusher to push messages

藏色散人
Release: 2020-01-26 14:32:53
forward
3534 people have browsed it

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
Copy after login

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 ], ], .....
Copy after login

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]; } }
Copy after login

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('测试'));
Copy after login

2. Front-end code

  Pusher Test   
Copy after login

ps:

1.pusher uses curl tohttps://pusher. comSubmit 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_VERIFYPEERandCURLOPT_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);
Copy after login
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!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!