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

Support reverb as a broadcasting driver #2639

Merged
merged 2 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ You can find and compare releases at the [GitHub release page](https://github.co

## Unreleased

### Added

- Support Laravel Reverb as a subscriptions broadcasting driver https://github.com/nuwave/lighthouse/pull/2639

## v6.46.0

### Added
Expand Down
6 changes: 3 additions & 3 deletions benchmarks/QueryBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public function setUp(): void
{
parent::setUp();

$configRepository = $this->app->make(ConfigRepository::class);
assert($configRepository instanceof ConfigRepository);
$config = $this->app->make(ConfigRepository::class);
assert($config instanceof ConfigRepository);

$routeName = $configRepository->get('lighthouse.route.name');
$routeName = $config->get('lighthouse.route.name');
$this->graphQLEndpoint = route($routeName);
}

Expand Down
3 changes: 1 addition & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,8 @@ parameters:
message: '#Call to method version\(\) on an unknown class Laravel\\Lumen\\Application.#'
- path: src/Subscriptions/SubscriptionRouter.php
messages:
- '#Parameter \$router of method Nuwave\\Lighthouse\\Subscriptions\\SubscriptionRouter::pusher\(\) has invalid type Laravel\\Lumen\\Routing\\Router\.#'
- '#Parameter \$router of method Nuwave\\Lighthouse\\Subscriptions\\SubscriptionRouter::.+\(\) has invalid type Laravel\\Lumen\\Routing\\Router\.#'
- '#Call to method post\(\) on an unknown class Laravel\\Lumen\\Routing\\Router\.#'
- '#Parameter \$router of method Nuwave\\Lighthouse\\Subscriptions\\SubscriptionRouter::echoRoutes\(\) has invalid type Laravel\\Lumen\\Routing\\Router\.#'
- path: src/Http/routes.php
messages:
- '#PHPDoc tag @var for variable \$router contains unknown class Laravel\\Lumen\\Routing\\Router\.#'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* @method \Symfony\Component\HttpFoundation\Response authorized(\Illuminate\Http\Request $request)
* @method \Symfony\Component\HttpFoundation\Response unauthorized(\Illuminate\Http\Request $request)
*/
class BroadcastManager extends DriverManager
class BroadcastDriverManager extends DriverManager
{
protected function configKey(): string
{
Expand All @@ -38,11 +38,9 @@
protected function createPusherDriver(array $config): PusherBroadcaster
{
$connection = $config['connection'] ?? 'pusher';
$driverConfig = config("broadcasting.connections.{$connection}");

if (empty($driverConfig) || $driverConfig['driver'] !== 'pusher') {
throw new \RuntimeException("Could not initialize Pusher broadcast driver for connection: {$connection}.");
}
$driverConfig = config("broadcasting.connections.{$connection}")
?? throw new \RuntimeException("Missing connection {$connection} from config/broadcasting.php.");

Check warning on line 43 in src/Subscriptions/BroadcastDriverManager.php

View check run for this annotation

Codecov / codecov/patch

src/Subscriptions/BroadcastDriverManager.php#L43

Added line #L43 was not covered by tests

$pusher = new Pusher(
$driverConfig['key'],
Expand Down
8 changes: 4 additions & 4 deletions src/Subscriptions/SubscriptionBroadcaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function __construct(
protected AuthorizesSubscriptions $subscriptionAuthorizer,
protected StoresSubscriptions $subscriptionStorage,
protected SubscriptionIterator $subscriptionIterator,
protected BroadcastManager $broadcastManager,
protected BroadcastDriverManager $broadcastDriverManager,
protected BusDispatcher $busDispatcher,
) {}

Expand Down Expand Up @@ -50,15 +50,15 @@ function (Subscriber $subscriber) use ($root): void {
$subscriber->variables,
$subscriber,
);
$this->broadcastManager->broadcast($subscriber, $result);
$this->broadcastDriverManager->broadcast($subscriber, $result);
},
);
}

public function authorize(Request $request): Response
{
return $this->subscriptionAuthorizer->authorize($request)
? $this->broadcastManager->authorized($request)
: $this->broadcastManager->unauthorized($request);
? $this->broadcastDriverManager->authorized($request)
: $this->broadcastDriverManager->unauthorized($request);
}
}
4 changes: 2 additions & 2 deletions src/Subscriptions/SubscriptionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
return $broadcaster->authorize($request);
}

public function webhook(Request $request, BroadcastManager $broadcastManager): Response
public function webhook(Request $request, BroadcastDriverManager $broadcastDriverManager): Response

Check warning on line 16 in src/Subscriptions/SubscriptionController.php

View check run for this annotation

Codecov / codecov/patch

src/Subscriptions/SubscriptionController.php#L16

Added line #L16 was not covered by tests
{
return $broadcastManager->hook($request);
return $broadcastDriverManager->hook($request);

Check warning on line 18 in src/Subscriptions/SubscriptionController.php

View check run for this annotation

Codecov / codecov/patch

src/Subscriptions/SubscriptionController.php#L18

Added line #L18 was not covered by tests
}
}
12 changes: 11 additions & 1 deletion src/Subscriptions/SubscriptionRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class SubscriptionRouter
{
/** Register the routes for pusher based subscriptions. */
/** Register the routes for Pusher based subscriptions. */
public function pusher(Registrar|Router $router): void
{
$router->post('graphql/subscriptions/auth', [
Expand All @@ -21,6 +21,16 @@
]);
}

/** Register the routes for Laravel Reverb based subscriptions. */
public function reverb(Registrar|Router $router): void

Check warning on line 25 in src/Subscriptions/SubscriptionRouter.php

View check run for this annotation

Codecov / codecov/patch

src/Subscriptions/SubscriptionRouter.php#L25

Added line #L25 was not covered by tests
{
$router->post('graphql/subscriptions/auth', [
'as' => 'lighthouse.subscriptions.auth',
'uses' => SubscriptionController::class . '@authorize',
]);

Check warning on line 30 in src/Subscriptions/SubscriptionRouter.php

View check run for this annotation

Codecov / codecov/patch

src/Subscriptions/SubscriptionRouter.php#L27-L30

Added lines #L27 - L30 were not covered by tests
}

/** Register the routes for Laravel Echo based subscriptions. */
public function echoRoutes(Registrar|Router $router): void
{
$router->post('graphql/subscriptions/auth', [
Expand Down
2 changes: 1 addition & 1 deletion src/Subscriptions/SubscriptionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class SubscriptionServiceProvider extends ServiceProvider
{
public function register(): void
{
$this->app->singleton(BroadcastManager::class);
$this->app->singleton(BroadcastDriverManager::class);
$this->app->singleton(SubscriptionRegistry::class);
$this->app->singleton(StoresSubscriptions::class, static function (Container $app): StoresSubscriptions {
$configRepository = $app->make(ConfigRepository::class);
Expand Down
8 changes: 4 additions & 4 deletions src/Testing/MakesGraphQLRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
use Illuminate\Support\Arr;
use Illuminate\Testing\TestResponse;
use Nuwave\Lighthouse\Http\Responses\MemoryStream;
use Nuwave\Lighthouse\Subscriptions\BroadcastDriverManager;
use Nuwave\Lighthouse\Subscriptions\Broadcasters\LogBroadcaster;
use Nuwave\Lighthouse\Subscriptions\BroadcastManager;
use Nuwave\Lighthouse\Subscriptions\Contracts\Broadcaster;
use Nuwave\Lighthouse\Support\Contracts\CanStreamResponse;
use PHPUnit\Framework\Assert;
Expand Down Expand Up @@ -246,11 +246,11 @@
$config->get('lighthouse.subscriptions.broadcasters.log'),
));

$broadcastManager = $app->make(BroadcastManager::class);
assert($broadcastManager instanceof BroadcastManager);
$broadcastDriverManager = $app->make(BroadcastDriverManager::class);

Check warning on line 249 in src/Testing/MakesGraphQLRequests.php

View check run for this annotation

Codecov / codecov/patch

src/Testing/MakesGraphQLRequests.php#L249

Added line #L249 was not covered by tests
assert($broadcastDriverManager instanceof BroadcastDriverManager);

// adding a custom driver which is a spied version of log driver
$broadcastManager->extend('mock', fn () => $this->spy(LogBroadcaster::class)->makePartial());
$broadcastDriverManager->extend('mock', fn () => $this->spy(LogBroadcaster::class)->makePartial());

Check warning on line 253 in src/Testing/MakesGraphQLRequests.php

View check run for this annotation

Codecov / codecov/patch

src/Testing/MakesGraphQLRequests.php#L253

Added line #L253 was not covered by tests

// set the custom driver as the default driver
$config->set('lighthouse.subscriptions.broadcaster', 'mock');
Expand Down
8 changes: 4 additions & 4 deletions src/Testing/MakesGraphQLRequestsLumen.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
use Illuminate\Support\Arr;
use Illuminate\Testing\TestResponse;
use Nuwave\Lighthouse\Http\Responses\MemoryStream;
use Nuwave\Lighthouse\Subscriptions\BroadcastDriverManager;
use Nuwave\Lighthouse\Subscriptions\Broadcasters\LogBroadcaster;
use Nuwave\Lighthouse\Subscriptions\BroadcastManager;
use Nuwave\Lighthouse\Subscriptions\Contracts\Broadcaster;
use Nuwave\Lighthouse\Support\Contracts\CanStreamResponse;
use PHPUnit\Framework\Assert;
Expand Down Expand Up @@ -265,11 +265,11 @@
$config->get('lighthouse.subscriptions.broadcasters.log'),
));

$broadcastManager = $app->make(BroadcastManager::class);
assert($broadcastManager instanceof BroadcastManager);
$broadcastDriverManager = $app->make(BroadcastDriverManager::class);

Check warning on line 268 in src/Testing/MakesGraphQLRequestsLumen.php

View check run for this annotation

Codecov / codecov/patch

src/Testing/MakesGraphQLRequestsLumen.php#L268

Added line #L268 was not covered by tests
assert($broadcastDriverManager instanceof BroadcastDriverManager);

// adding a custom driver which is a spied version of log driver
$broadcastManager->extend('mock', fn () => $this->spy(LogBroadcaster::class)->makePartial());
$broadcastDriverManager->extend('mock', fn () => $this->spy(LogBroadcaster::class)->makePartial());

Check warning on line 272 in src/Testing/MakesGraphQLRequestsLumen.php

View check run for this annotation

Codecov / codecov/patch

src/Testing/MakesGraphQLRequestsLumen.php#L272

Added line #L272 was not covered by tests

// set the custom driver as the default driver
$config->set('lighthouse.subscriptions.broadcaster', 'mock');
Expand Down
17 changes: 8 additions & 9 deletions src/Testing/TestResponseMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@
use Illuminate\Testing\TestResponse;
use Mockery\LegacyMockInterface;
use Mockery\MockInterface;
use Nuwave\Lighthouse\Subscriptions\BroadcastManager;
use Nuwave\Lighthouse\Subscriptions\BroadcastDriverManager;
use Nuwave\Lighthouse\Subscriptions\Contracts\Broadcaster;
use Nuwave\Lighthouse\Subscriptions\Subscriber;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;

/**
* @mixin \Illuminate\Testing\TestResponse
*/
/** @mixin \Illuminate\Testing\TestResponse */
class TestResponseMixin
{
public function assertGraphQLValidationError(): \Closure
Expand Down Expand Up @@ -155,12 +153,13 @@ public function assertGraphQLSubscriptionNotAuthorized(): \Closure
public function graphQLSubscriptionMock(): \Closure
{
return function (): MockInterface {
$broadcastManager = Container::getInstance()->make(BroadcastManager::class);
assert($broadcastManager instanceof BroadcastManager);
$mock = $broadcastManager->driver();
assert($mock instanceof Broadcaster && $mock instanceof MockInterface);
$broadcastDriverManager = Container::getInstance()->make(BroadcastDriverManager::class);
assert($broadcastDriverManager instanceof BroadcastDriverManager);

return $mock;
$broadcastDriver = $broadcastDriverManager->driver();
assert($broadcastDriver instanceof Broadcaster && $broadcastDriver instanceof MockInterface);

return $broadcastDriver;
};
}

Expand Down
12 changes: 5 additions & 7 deletions src/Testing/TestsSubscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@

use Illuminate\Container\Container;
use Illuminate\Contracts\Config\Repository as ConfigRepository;
use Nuwave\Lighthouse\Subscriptions\BroadcastDriverManager;
use Nuwave\Lighthouse\Subscriptions\Broadcasters\LogBroadcaster;
use Nuwave\Lighthouse\Subscriptions\BroadcastManager;
use Nuwave\Lighthouse\Subscriptions\Contracts\Broadcaster;

/**
* Sets up the environment for testing subscriptions.
*/
/** Sets up the environment for testing subscriptions. */
trait TestsSubscriptions
{
protected function setUpTestsSubscriptions(): void
Expand All @@ -27,11 +25,11 @@ protected function setUpTestsSubscriptions(): void
$config->get('lighthouse.subscriptions.broadcasters.log'),
));

$broadcastManager = $app->make(BroadcastManager::class);
assert($broadcastManager instanceof BroadcastManager);
$broadcastDriverManager = $app->make(BroadcastDriverManager::class);
assert($broadcastDriverManager instanceof BroadcastDriverManager);

// adding a custom driver which is a spied version of log driver
$broadcastManager->extend('mock', fn () => $this->spy(LogBroadcaster::class)->makePartial());
$broadcastDriverManager->extend('mock', fn () => $this->spy(LogBroadcaster::class)->makePartial());

// set the custom driver as the default driver
$config->set('lighthouse.subscriptions.broadcaster', 'mock');
Expand Down
15 changes: 10 additions & 5 deletions src/lighthouse.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,16 +435,21 @@
'log' => [
'driver' => 'log',
],
'pusher' => [
'driver' => 'pusher',
'routes' => Nuwave\Lighthouse\Subscriptions\SubscriptionRouter::class . '@pusher',
'connection' => 'pusher',
],
'echo' => [
'driver' => 'echo',
'connection' => env('LIGHTHOUSE_SUBSCRIPTION_REDIS_CONNECTION', 'default'),
'routes' => Nuwave\Lighthouse\Subscriptions\SubscriptionRouter::class . '@echoRoutes',
],
'pusher' => [
'driver' => 'pusher',
'connection' => 'pusher',
'routes' => Nuwave\Lighthouse\Subscriptions\SubscriptionRouter::class . '@pusher',
],
'reverb' => [
'driver' => 'pusher',
'connection' => 'reverb',
'routes' => Nuwave\Lighthouse\Subscriptions\SubscriptionRouter::class . '@reverb',
],
],

/*
Expand Down
Loading
Loading