Laravel結合Pusher實現數據實時推送

一、註冊pusher賬戶並創建一個應用

打開網址: https://pusher.com註冊獲取應用祕鑰

二、安裝配置Pusher

1.安裝

composer require pusher/pusher-php-server

2.打開config/broadcasting.php 進行如下配置

'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','ap3'),
       'useTLS' => true,
       'curl_options' => [
           CURLOPT_SSL_VERIFYHOST => 0,
            CURLOPT_SSL_VERIFYPEER => 0,
        ]
     ],
   ],
]

3.配置ENV

BROADCAST_DRIVER=pusher
PUSHER_APP_ID=xxxxxxx
PUSHER_APP_KEY=xxxxxxx
PUSHER_APP_SECRET=xxxxxxx
PUSHER_APP_CLUSTER=xxxxxxx

三、事件

1.創建Pusher

php artisan make:event PusherEvent
<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class PusherEvent implements ShouldBroadcast
{
    use SerializesModels;

    public $info;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($info)
    {
       $this->info = $info;
    }


    /**
     * 指定廣播頻道
     * @return string[]
     */
    public function broadcastOn()
    {
        return ['my-channel'];
    }


    /**
     * 指定廣播事件
     * @return string
     */
    public function broadcastAs()
    {
        return 'my-event';
    }


    public function  broadcastWith()
    {
        return ['info' => $this->info];
    }
}

四、觸發上面的測試事件

1.控制器中觸發event(new PusherEvent('測試'));

<?php

namespace App\Http\Controllers;

use App\Events\PusherEvent;
use Illuminate\Http\Request;

class HomeController extends Controller
{

    public function index()
    {
        return view('index');
    }

    public function test(Request $request)
    {
        try {
            event(new PusherEvent('測試'));
            dd('success');
        } catch (\Exception $exception) {
            dd($exception->getMessage());
        }
    }
}

2.也可以使用artisan 命令來觸發

php artisan make:command PusherEventCommand

找到 app/Console/Commands/PusherEventCommand.php 

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class PusherEventCommand extends Command
{

    protected $signature = 'pusher:test {message}';
    protected $description = 'pusher test';

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        event(new \App\Events\PusherEvent($this->argument('message')));
    }

}

找到 app/Console/Kernel.php 將創建的命令添加到 $commands中

protected $commands = [
      \App\Console\Commands\PusherEventCommand::class,
];

到這裏就配置完了 ,可以使用artisan命令測試一下

php artisan pusher:test "這是一個命令測試"

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章