订阅的一个常见用例是您不想向触发广播的人广播。 Laravel Lighthouse的示例代码做了类似的事情,但是希望您将用户ID存储在数据库中。
请参见docs
/**
* Filter which subscribers should receive the subscription.
*
* @param \Nuwave\Lighthouse\Subscriptions\Subscriber $subscriber
* @param mixed $root
* @return bool
*/
public function filter(Subscriber $subscriber, $root): bool
{
$user = $subscriber->context->user;
// Don't broadcast the subscription to the same
// person who updated the post.
return $root->updated_by !== $user->id;
}
这是一个“天真的”假设。同一用户可能正在多个选项卡中工作,并且假定用户ID已存储。
如果我们能拥有类似Laravel Echo的“ toOthers”这样的东西,那将不是一件好事
broadcast(new ShippingStatusUpdated($update))->toOthers();
基本上,您需要在触发广播时获取套接字ID,然后使用filter方法对其进行过滤。还是以某种方式已经可能?