Open
Description
What problem does this feature proposal attempt to solve?
When broadcasting subscriptions you can filter out the user that triggered the broadcast with:
public function filter(Subscriber $subscriber, mixed $root): bool
{
return $subscriber->socket_id !== request()->header('X-Socket-ID');
}
The problem comes when the broadcast is queued instead of dispatching it synchronously, when queued request()
is always empty and the filter will be ignorred.
Which possible solutions should be considered?
One solution could be passing the request to the BroadcastSubscriptionJob
and setting it on execution:
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Facade;
class BroadcastSubscriptionJob implements ShouldQueue
{
use Queueable;
use SerializesModels;
public function __construct(
/**
* The subscription field that was requested.
*/
public GraphQLSubscription $subscription,
/**
* The name of the field.
*/
public string $fieldName,
/**
* The root element to be passed when resolving the subscription.
*/
public mixed $root,
/**
* The request context when the broadcast was triggered
*/
public Request $request,
) {}
public function handle(BroadcastsSubscriptions $broadcaster): void
{
App::instance('request', $this->request);
Facade::clearResolvedInstance('request');
$broadcaster->broadcast($this->subscription, $this->fieldName, $this->root);
}
}
Other solution could be adding a "native" toOthers
functionality to the broadcaster:
public function queueBroadcast(GraphQLSubscription $subscription, string $fieldName, mixed $root, bool $toOthers = false): void
{
$broadcastSubscriptionJob = new BroadcastSubscriptionJob($subscription, $fieldName, $root, $toOthers);
$broadcastSubscriptionJob->onQueue(config('lighthouse.subscriptions.broadcasts_queue_name'));
$this->busDispatcher->dispatch($broadcastSubscriptionJob);
}
public function broadcast(GraphQLSubscription $subscription, string $fieldName, mixed $root, bool $toOthers = false): void
{
$topic = $subscription->decodeTopic($fieldName, $root);
$subscribers = $this->subscriptionStorage
->subscribersByTopic($topic)
->when($toOthers && Broadcast::socket())
->filter(static fn (Subscriber $subscriber): bool => $subscriber->socket_id !== Broadcast::socket())
->filter(static fn (Subscriber $subscriber): bool => $subscription->filter($subscriber, $root));
$this->subscriptionIterator->process(
$subscribers,
function (Subscriber $subscriber) use ($root): void {
$subscriber->root = $root;
$result = $this->graphQL->executeParsedQuery(
$subscriber->query,
$subscriber->context,
$subscriber->variables,
$subscriber,
);
$this->broadcastManager->broadcast($subscriber, $result);
},
);
}
The problem with this solution is that it could break compatibility with the BroadcastsSubscriptions
interface.