Thomas Timmermans
Thomas Timmermans

Reputation: 402

Laravel Pusher: Get all users connected to Presence Channel

Does anyone know how te get all connected users from a Presence channel? I need them in my API Controller and save all of them in a database.

I found some people use Pusher::get() but then I get an error saying Non-static method Pusher\Pusher::get() should not be called statically. I've seen others use $this->pusher('/channels'), but where do I get the $pusher instance from?

Sorry, maybe a noob question?

Upvotes: 3

Views: 3280

Answers (1)

SebSob
SebSob

Reputation: 582

$pusher is the actual Pusher SDK instance, and you'll have to instantiate it yourself:

$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');

Btw, this is also how Laravel does it internally if you look here.

Upvotes: 5

Related Questions