From 5b6bdf49e46eead770d8cbab0ed7db53849561f3 Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Fri, 28 Aug 2020 19:44:54 +0300 Subject: [PATCH] Added configurable client for each replication driver. --- config/websockets.php | 31 ++++++++++++++++++++++++++++ src/Console/StartWebSocketServer.php | 21 ++++++++----------- tests/TestCase.php | 23 ++++++++++----------- 3 files changed, 51 insertions(+), 24 deletions(-) diff --git a/config/websockets.php b/config/websockets.php index 28b14b4..e45b012 100644 --- a/config/websockets.php +++ b/config/websockets.php @@ -166,10 +166,41 @@ return [ 'driver' => env('LARAVEL_WEBSOCKETS_REPLICATION_DRIVER', 'local'), + /* + |-------------------------------------------------------------------------- + | Local Replication + |-------------------------------------------------------------------------- + | + | Local replication is actually a null replicator, meaning that it + | is the default behaviour of storing the connections into an array. + | + */ + + 'local' => [ + + 'client' => \BeyondCode\LaravelWebSockets\PubSub\Drivers\LocalClient::class, + + ], + + /* + |-------------------------------------------------------------------------- + | Redis Replication + |-------------------------------------------------------------------------- + | + | Redis replication relies on the Redis' Pub/Sub protocol. When users + | are connected across multiple nodes, whenever some event gets triggered + | on one instance, the rest of the instances get the same copy and, in + | case the connected users to other instances are valid to receive + | the event, they will receive it. + | + */ + 'redis' => [ 'connection' => env('LARAVEL_WEBSOCKETS_REPLICATION_CONNECTION', 'default'), + 'client' => \BeyondCode\LaravelWebSockets\PubSub\Drivers\RedisClient::class, + ], ], diff --git a/src/Console/StartWebSocketServer.php b/src/Console/StartWebSocketServer.php index d6c4dcb..fcf0737 100644 --- a/src/Console/StartWebSocketServer.php +++ b/src/Console/StartWebSocketServer.php @@ -4,8 +4,6 @@ namespace BeyondCode\LaravelWebSockets\Console; use BeyondCode\LaravelWebSockets\Facades\StatisticsLogger; use BeyondCode\LaravelWebSockets\Facades\WebSocketsRouter; -use BeyondCode\LaravelWebSockets\PubSub\Drivers\LocalClient; -use BeyondCode\LaravelWebSockets\PubSub\Drivers\RedisClient; use BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface; use BeyondCode\LaravelWebSockets\Server\Logger\ConnectionLogger; use BeyondCode\LaravelWebSockets\Server\Logger\HttpLogger; @@ -189,17 +187,16 @@ class StartWebSocketServer extends Command */ public function configurePubSub() { - if (config('websockets.replication.driver', 'local') === 'local') { - $this->laravel->singleton(ReplicationInterface::class, function () { - return new LocalClient; - }); - } + $this->laravel->singleton(ReplicationInterface::class, function () { + $driver = config('websockets.replication.driver', 'local'); - if (config('websockets.replication.driver', 'local') === 'redis') { - $this->laravel->singleton(ReplicationInterface::class, function () { - return (new RedisClient)->boot($this->loop); - }); - } + $client = config( + "websockets.replication.{$driver}.client", + \BeyondCode\LaravelWebSockets\PubSub\Drivers\LocalClient::class + ); + + return (new $client)->boot($this->loop); + }); $this->laravel ->get(ReplicationInterface::class) diff --git a/tests/TestCase.php b/tests/TestCase.php index e817fb8..3c13e4a 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -258,19 +258,18 @@ abstract class TestCase extends BaseTestCase { // Replace the publish and subscribe clients with a Mocked // factory lazy instance on boot. - if (config('websockets.replication.driver') === 'redis') { - $this->app->singleton(ReplicationInterface::class, function () { - return (new RedisClient)->boot( - LoopFactory::create(), Mocks\RedisFactory::class - ); - }); - } + $this->app->singleton(ReplicationInterface::class, function () { + $driver = config('websockets.replication.driver', 'local'); - if (config('websockets.replication.driver') === 'local') { - $this->app->singleton(ReplicationInterface::class, function () { - return new LocalClient; - }); - } + $client = config( + "websockets.replication.{$driver}.client", + \BeyondCode\LaravelWebSockets\PubSub\Drivers\LocalClient::class + ); + + return (new $client)->boot( + LoopFactory::create(), Mocks\RedisFactory::class + ); + }); } protected function runOnlyOnRedisReplication()