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 9 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
Stevemoretz marked this conversation as resolved.
Show resolved Hide resolved

### Fixed

Expand Down
35 changes: 35 additions & 0 deletions docs/5/subscriptions/filtering-subscriptions.md
Original file line number Diff line number Diff line change
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 to a given channel except for the current user. You may accomplish this using the filter function, this following snippet is the equivalent of [toOthers method from Laravel's broadcast helper](https://laravel.com/docs/9.x/broadcasting#only-to-others).
Stevemoretz marked this conversation as resolved.
Show resolved Hide resolved

```php
namespace App\GraphQL\Subscriptions;

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

class PostUpdatedSubscription extends GraphQLSubscription
Stevemoretz marked this conversation as resolved.
Show resolved Hide resolved
{
/**
* 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
Stevemoretz marked this conversation as resolved.
Show resolved Hide resolved
{
// Filter out the sender
return $subscriber->socket_id !== request()->header("x-socket-id");
Stevemoretz marked this conversation as resolved.
Show resolved Hide resolved
}
}
```

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 as a `X-Socket-ID` header. Then, you can access that in your filter function.
Stevemoretz marked this conversation as resolved.
Show resolved Hide resolved

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:
Stevemoretz marked this conversation as resolved.
Show resolved Hide resolved

```js
var socketId = Echo.socketId();
Stevemoretz marked this conversation as resolved.
Show resolved Hide resolved
```
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.
Stevemoretz marked this conversation as resolved.
Show resolved Hide resolved
*/
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');
Stevemoretz marked this conversation as resolved.
Show resolved Hide resolved

$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