Queued subscriptions to others
Nobody has claimed this yet.
- Dominant language
- PHP
- Stars
- 3.5k
- Forks
- 468
- Avg merge
- 3h 9m
- Merged PRs (30d)
- 2
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading the BroadcastSubscriptionJob and the BroadcastsSubscriptions interface, then trace queueBroadcast and broadcast to understand how the request-based filter currently behaves. Compare preserving the request context with adding native to-others behavior, and consider compatibility with the existing interface. Done means queued broadcasts can exclude the triggering subscriber without breaking existing implementations.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- graphql, laravel, php
- Domain
- api, backend, backend-api-design
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100