Laravel-websockets: Pusher API ? How to query channels / users in channels ?

Created on 23 Dec 2018  路  8Comments  路  Source: beyondcode/laravel-websockets

In my current video chat app, as part of the admin dashboard, it's possible to query the rest api which in turn queries Pusher to ask how many users are in which channels without having to actually join the channels on the client side.. like this:

    public function channelStats(Request $request)
    {
        $users = User::get(['id', 'name', 'gender'])->keyBy('id');

        $usersInOnline = Pusher::get('/channels/presence-online/users');
        $usersInLobby = Pusher::get('/channels/presence-lobby/users');
        $usersInVideo = Pusher::get('/channels/presence-video/users');

        $onlineUserIds = collect($usersInOnline['result']['users'])->map(function ($user) { return $user['id']; });
        $lobbyUserIds = collect($usersInLobby['result']['users'])->map(function ($user) { return $user['id']; });
        $videoUserIds = collect($usersInVideo['result']['users'])->map(function ($user) { return $user['id']; });

        return [
            'online' => count($usersInOnline['result']['users']),
            'lobby' => count($usersInLobby['result']['users']),
            'video' => count($usersInVideo['result']['users']),
            'users' => [
                'online' => $users->whereIn('id', $onlineUserIds),
                'lobby' => $users->whereIn('id', $lobbyUserIds),
                'video' => $users->whereIn('id', $videoUserIds),
            ],
        ];
    }

My question is.. how could I achieve this with laravel-websockets ?

All 8 comments

We offer the same API as Pusher does. I think you can even tell the pusher PHP SDK to use a different host, but I鈥檓 not sure how.

See
https://github.com/beyondcode/laravel-websockets/blob/master/src/Server/Router.php#L37

Got it to work by building up the connection manually rather than using the Pusher:: instance, and then specifying the actual host rather than using 127.0.0.1

        $connection = config('broadcasting.connections.pusher');

            $pusher = new Pusher\Pusher(
                $connection['key'],
                $connection['secret'],
                $connection['app_id'],
                [
                    'cluster' => $connection['options']['cluster'],
                    'useTLS' => true,
                    'curl_options' => $connection['options']['curl_options'],
                    'host' => 'socketmaster.dev',
                    'port' => '6001',
                    'scheme' => 'https',
                ]
            );

$channels = $pusher->get('/channels')['result']['channels'];

It would be useful to know however if it's now possible to query this directly from Laravel Echo and if so.. how ?

This doesn't work for me. How i can check from php the presence channels?

maybe if you post your code? what error are you getting?

$connection = config( 'broadcasting.connections.pusher' );

    $pusher = new Pusher\Pusher(
        $connection['key'],
        $connection['secret'],
        $connection['app_id'],
        [
            'cluster' => $connection['options']['cluster'],
            'useTLS'  => TRUE,
            'host'    => 'chat.dev',
            'port'    => '6001',
            'scheme'  => 'https',
            'debug'   => TRUE,
        ]
    );

    $channels = $pusher->get_channels();

   dd($channels); // i get false

Dashboard and echo is working

what about your curl_options?

                'curl_options' => [
                    CURLOPT_SSL_VERIFYHOST => 0,
                    CURLOPT_SSL_VERIFYPEER => 0,
                ],

and why is your call to get_channels() different from mine?

$channels = $pusher->get('/channels');
dd($channels); // gives what?

Thanks. That was the issue.

Is this still viable?
I'm struggling fetching any data regarding current channels and users.

I have tried all sorts of code such as code blocks provided above and fetching that information through AppProvider & ChannelManagers without success.

Was this page helpful?
0 / 5 - 0 ratings