Laravel Pusher:让所有用户连接到 Presence Channel

Tho*_*ans 3 laravel pusher laravel-echo

有谁知道如何从状态通道获取所有连接的用户?我的 API 控制器中需要它们,并将它们全部保存在数据库中。

我发现有些人使用Pusher::get(),但后来我收到一条错误消息Non-static method Pusher\Pusher::get() should not be called statically。我见过其他人使用$this->pusher('/channels'),但是我从哪里获取 $pusher 实例?

抱歉,也许是个菜鸟问题?

Seb*_*Sob 5

$pusher是实际的 Pusher SDK 实例,您必须自己实例化它:

$connection = config('broadcasting.connections.pusher');
$pusher = new Pusher(
    $connection['key'],
    $connection['secret'],
    $connection['app_id'],
    $connection['options'] ?? []
);

// Example 1: get all active channels
$channels = $pusher->get_channels();

// Example 2: Get all the connected users for a specific channel
$users = $pusher->get('/channels/<channel-name>/users');
Run Code Online (Sandbox Code Playgroud)

顺便说一句,如果你看这里,这也是 Laravel 内部的做法。