前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Laravel结合Pusher实现数据实时推送

Laravel结合Pusher实现数据实时推送

原创
作者头像
素描
修改2021-11-12 11:09:32
4180
修改2021-11-12 11:09:32
举报
文章被收录于专栏:编程录编程录

一、注册pusher账户并创建一个应用

打开网址: https://pusher.com注册获取应用秘钥

二、安装配置Pusher

1.安装

代码语言:javascript
复制
composer require pusher/pusher-php-server

2.打开config/broadcasting.php 进行如下配置

代码语言:javascript
复制
'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

代码语言:javascript
复制
BROADCAST_DRIVER=pusher
PUSHER_APP_ID=xxxxxxx
PUSHER_APP_KEY=xxxxxxx
PUSHER_APP_SECRET=xxxxxxx
PUSHER_APP_CLUSTER=xxxxxxx

三、事件

1.创建Pusher

代码语言:javascript
复制
php artisan make:event PusherEvent
代码语言:javascript
复制
<?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('测试'));

代码语言:javascript
复制
<?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 命令来触发

代码语言:javascript
复制
php artisan make:command PusherEventCommand

找到 app/Console/Commands/PusherEventCommand.php 

代码语言:javascript
复制
<?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中

代码语言:javascript
复制
protected $commands = [
      \App\Console\Commands\PusherEventCommand::class,
];

到这里就配置完了 ,可以使用artisan命令测试一下

代码语言:javascript
复制
php artisan pusher:test "这是一个命令测试"

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档