Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subscription send only to others #2298

Merged
merged 27 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
54ace3f
Update PaginateDirective.php
Stevemoretz Feb 7, 2023
11d785f
Cleanup
Stevemoretz Feb 7, 2023
7b2daa0
Adding a test
Stevemoretz Feb 7, 2023
7c4d47f
Update tests/Unit/Pagination/PaginateDirectiveTest.php
Stevemoretz Feb 7, 2023
3ea8d0f
Adding a test
Stevemoretz Feb 7, 2023
3a598a6
Initial
Stevemoretz Feb 14, 2023
3e65d77
Merge branch 'master' into sendToOthers
Stevemoretz Feb 14, 2023
3c2e6a2
Delete schema-cache-refreshing
Stevemoretz Feb 14, 2023
1e8d64a
Update CHANGELOG.md
Stevemoretz Feb 14, 2023
98ced0a
Update CHANGELOG.md
Stevemoretz Feb 14, 2023
e548e3d
Update docs/5/subscriptions/filtering-subscriptions.md
Stevemoretz Feb 14, 2023
a54c7a7
Update docs/5/subscriptions/filtering-subscriptions.md
Stevemoretz Feb 14, 2023
53c562f
Update docs/5/subscriptions/filtering-subscriptions.md
Stevemoretz Feb 14, 2023
250a5c9
Update docs/5/subscriptions/filtering-subscriptions.md
Stevemoretz Feb 14, 2023
7d43c6b
Update docs/5/subscriptions/filtering-subscriptions.md
Stevemoretz Feb 14, 2023
f49bb38
Update docs/5/subscriptions/filtering-subscriptions.md
Stevemoretz Feb 14, 2023
58d0a91
Update docs/5/subscriptions/filtering-subscriptions.md
Stevemoretz Feb 14, 2023
9b80342
Update src/Subscriptions/Subscriber.php
Stevemoretz Feb 14, 2023
b17e033
Update src/Subscriptions/Subscriber.php
Stevemoretz Feb 14, 2023
4a0e3fc
Swap docs from v5 to master
Stevemoretz Feb 14, 2023
c770cda
update
Stevemoretz Feb 14, 2023
2513d63
Add test
Stevemoretz Feb 14, 2023
6bc932b
Might fix stan
Stevemoretz Feb 14, 2023
9feb4d2
use assert instead of @var
Stevemoretz Feb 14, 2023
0f87f84
use assert instead of @var
Stevemoretz Feb 14, 2023
55de3cc
update
Stevemoretz Feb 14, 2023
46b461b
Merge remote-tracking branch 'upstream/master' into sendToOthers
Stevemoretz Feb 14, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ You can find and compare releases at the [GitHub release page](https://github.co
- Require implementations of `BatchedEntityResolver` to maintain the keys given in `array $representations` https://github.com/nuwave/lighthouse/pull/2286
- Use the strongest possible property types over PHPDocs
- Require filter directives such as `@whereKey` in `@delete`, `@forceDelete` and `@restore` https://github.com/nuwave/lighthouse/pull/2289
- Subscriptions can now be filtered via `$subscriber->socket_id` and `request()->header('X-Socket-ID')` https://github.com/nuwave/lighthouse/pull/2298

### Fixed

Expand Down
10 changes: 5 additions & 5 deletions docs/5/subscriptions/filtering-subscriptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ There are times when you'll need to filter out specific events based on the argu

```graphql
subscription onPostUpdated($post_id: ID!) {
postUpdated(post_id: $post_id) {
id
title
content
}
postUpdated(post_id: $post_id) {
id
Stevemoretz marked this conversation as resolved.
Show resolved Hide resolved
title
content
}
}
```

Expand Down
45 changes: 40 additions & 5 deletions docs/master/subscriptions/filtering-subscriptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ There are times when you'll need to filter out specific events based on the argu

```graphql
subscription onPostUpdated($post_id: ID!) {
postUpdated(post_id: $post_id) {
id
title
content
}
postUpdated(post_id: $post_id) {
id
Stevemoretz marked this conversation as resolved.
Show resolved Hide resolved
title
content
}
}
```

Expand Down Expand Up @@ -40,3 +40,38 @@ class PostUpdatedSubscription extends GraphQLSubscription
}
}
```

## Only To Others

When building an application that utilizes event broadcasting, you may occasionally need to broadcast an event to all subscribers of a channel except for the current user.
You may accomplish this using the filter function, this following snippet is equivalent to [the `toOthers()` method from Laravel's broadcast helper](https://laravel.com/docs/9.x/broadcasting#only-to-others).

```php
namespace App\GraphQL\Subscriptions;

use Nuwave\Lighthouse\Subscriptions\Subscriber;
use Nuwave\Lighthouse\Schema\Types\GraphQLSubscription;

final class PostUpdatedSubscription extends GraphQLSubscription
{
/**
* Filter which subscribers should receive the subscription.
*/
public function filter(Subscriber $subscriber, mixed $root): bool
{
// Filter out the sender
return $subscriber->socket_id !== request()->header('X-Socket-ID');
}
}
```

When you initialize a Laravel Echo instance, a socket ID is assigned to the connection.
If you are using a global [Axios](https://github.com/mzabriskie/axios) instance to make HTTP requests from your JavaScript application, the socket ID will automatically be attached to every outgoing request in the `X-Socket-ID` header.
Then, you can access that in your filter function.

If you are not using a global Axios instance, you will need to manually configure your JavaScript application to send the `X-Socket-ID` header with all outgoing requests.
You may retrieve the socket ID using the `Echo.socketId()` method:

```js
const socketId = Echo.socketId();
```
8 changes: 8 additions & 0 deletions src/Subscriptions/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ class Subscriber
*/
public $channel;

/**
* X-Socket-ID header passed on the subscription query.
*/
public ?string $socket_id;

/**
* The topic subscribed to.
*
Expand Down Expand Up @@ -90,6 +95,7 @@ public function __construct(
$this->args = $args;
$this->variables = $resolveInfo->variableValues;
$this->context = $context;
$this->socket_id = request()->header('X-Socket-ID');

$operation = $resolveInfo->operation;
assert($operation instanceof OperationDefinitionNode, 'Must be here, since webonyx/graphql-php validated the subscription.');
Expand All @@ -108,6 +114,7 @@ public function __construct(
public function __serialize(): array
{
return [
'socket_id' => $this->socket_id,
'channel' => $this->channel,
'topic' => $this->topic,
'query' => serialize(
Expand All @@ -133,6 +140,7 @@ public function __unserialize(array $data): void
);
assert($documentNode instanceof DocumentNode, 'We know the type since it is set during construction and serialized.');

$this->socket_id = $data['socket_id'];
$this->query = $documentNode;
$this->fieldName = $data['field_name'];
$this->args = $data['args'];
Expand Down