Laravel Broadcast Channels - check connections

﹥>﹥吖頭↗ 提交于 2020-06-27 09:53:23

问题


I can't find this in the docs or by searching, maybe someone has some tips. I'm trying to check how many connections are on a presence channel on the backend.

I can check fine on the front-end with Echo like so:

Echo.join('chat')
    .here((users) => {
        // users.length is the proper count of connections
    })

But is there a way I can get that same number of connections, but in the backend code somewhere within Laravel?


回答1:


If you are using Pusher, the backend can just do the following:

$response = $pusher->get( '/channels/presence-channel-name/users' );
if( $response[ 'status'] == 200 ) {
  // convert to associative array for easier consumption
  $users = json_decode( $response[ 'body' ], true )[ 'users' ];
}

$userCount = count($users);

You can read more about it in the pusher documentation. The pusher-http-php sdk also has some documentation for this.

A list of users present on a presence channel can be retrieved by querying the /channels/[channel_name]/users resource where the channel_name is replaced with a valid presence channel name.

This is explicitly only for presence channels.

Additionally, you can keep track of users in channels through webhooks.

Notify your application whenever a user subscribes to or unsubscribes from a Presence channel. For example, this allows you to synchronise channel presence state on your server as well as all your application clients.

Pusher will hit your server with information in the following form:

{
  "name": "member_added", // or "member_removed"
  "channel": "presence-your_channel_name",
  "user_id": "a_user_id"
}

This data could potentially be stored in a table in your database or alternatively in redis.




回答2:


I don't think it's possible. The channels are between the client (website with JS) and the WebSocket-Server (own NodeJS-Server or the Pusher-Servers). Laravel is just pushing events to them but is never pulling.

To find a solution we have to know which driver you are using (redis or pusher). It's maybe possible to ask the pusher server with curl how many users are on the server.

For pusher this looks interesting: https://support.pusher.com/hc/en-us/articles/204113596-Showing-who-s-online-with-a-large-number-of-users and https://pusher.com/docs/rest_api#method-get-channels

For Redis you could implement some logic inside the NodeJS server to listen to the channels and fire a request to laravel.

The best solution to be independent is to fire a request from each client to update the counter in your database:

Echo.join('chat')
.here((users) => {
    //request to laravel api with users.length
})

The disadvantage of this method is, that it only updates the value when a user connects to the channel




回答3:


Or could be this one as response

{ "user_id": "a_user_id" "name": "member_added", // or "member_removed" "channel": "presence-your_channel_name", }



来源:https://stackoverflow.com/questions/44077177/laravel-broadcast-channels-check-connections

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!