Home  >  Article  >  PHP Framework  >  Detailed explanation of how Laravel uses pusher to push messages

Detailed explanation of how Laravel uses pusher to push messages

藏色散人
藏色散人forward
2020-01-26 14:32:533619browse

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 [&#39;my-channel&#39;];
    }
 
    /**
     * 指定广播事件(对应前端的事件)
     * @return string
     */
    public function broadcastAs()
    {
        return &#39;my-event&#39;;
    }
 
    /**
     * 获取广播数据,默认是广播的public属性的数据
     */
    public function broadcastWith()
    {
        return [&#39;info&#39; => $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(&#39;测试&#39;));

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&#39;t include this in production
    Pusher.logToConsole = true;
 
    var pusher = new Pusher(&#39;XXX&#39;, {
      cluster: &#39;ap1&#39;,
      encrypted: true
    });
 
    var channel = pusher.subscribe(&#39;my-channel&#39;);
    channel.bind(&#39;my-event&#39;, 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!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete