简体   繁体   English

一旦用户在 Laravel 中使用 Redis 加入广播频道,在后端获取事件

[英]Get an event on backend side once a user has joined a broadcast channel with Redis in Laravel

I'm trying to understand how to catch an event backend side once a user joined the channel.我试图了解如何在用户加入频道后捕获事件后端。 I mean right after the authentication has been done for a Private or a Presence channel.我的意思是在对私有或在线通道进行身份验证之后。

For instance, after that:例如,在那之后:

Broadcast::channel('chat.{roomId}', function ($user, $roomId) {
    if ($user->canJoinRoom($roomId)) {
        return ['id' => $user->id, 'name' => $user->name];
    }
});

successfully proceeds, I would expect to have the possibility to catch an event to know that the user joined the channel (not only frontend side).成功进行,我希望有可能捕捉到一个事件来知道用户加入了频道(不仅仅是前端)。

Any hint on how to handle it?关于如何处理它的任何提示?

Reference: https://laravel.com/docs/6.x/broadcasting#joining-presence-channels参考: https : //laravel.com/docs/6.x/broadcasting#joining-presence-channels

To handle the event on the backend, the client needs to communicate when they join a channel.为了在后端处理事件,客户端需要在加入频道时进行通信。

axios.post('/channel/join')
     .then(res => res.data)
     .then(data => console.log);

On the backend, you can have a route in (eg) routes/web.php :在后端,您可以在(例如) routes/web.php有一个路由:

Route::post('/channel/join', 'ChannelController@joined');

Then, in ChannelController.php your can process the action and do what you want:然后,在ChannelController.php您可以处理操作并执行您想要的操作:

public function joined(Request $request)
{
    $user = auth()->user();

    broadcast(new UserJoinedChannel($user))->toOthers();

    return ['status' => 'Channel joined successfully!'];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM