Added configurable client for each replication driver.

This commit is contained in:
Alex Renoki 2020-08-28 19:44:54 +03:00
parent 00faff7f04
commit 5b6bdf49e4
3 changed files with 51 additions and 24 deletions

View File

@ -166,10 +166,41 @@ return [
'driver' => env('LARAVEL_WEBSOCKETS_REPLICATION_DRIVER', 'local'), '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' => [ 'redis' => [
'connection' => env('LARAVEL_WEBSOCKETS_REPLICATION_CONNECTION', 'default'), 'connection' => env('LARAVEL_WEBSOCKETS_REPLICATION_CONNECTION', 'default'),
'client' => \BeyondCode\LaravelWebSockets\PubSub\Drivers\RedisClient::class,
], ],
], ],

View File

@ -4,8 +4,6 @@ namespace BeyondCode\LaravelWebSockets\Console;
use BeyondCode\LaravelWebSockets\Facades\StatisticsLogger; use BeyondCode\LaravelWebSockets\Facades\StatisticsLogger;
use BeyondCode\LaravelWebSockets\Facades\WebSocketsRouter; 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\PubSub\ReplicationInterface;
use BeyondCode\LaravelWebSockets\Server\Logger\ConnectionLogger; use BeyondCode\LaravelWebSockets\Server\Logger\ConnectionLogger;
use BeyondCode\LaravelWebSockets\Server\Logger\HttpLogger; use BeyondCode\LaravelWebSockets\Server\Logger\HttpLogger;
@ -189,17 +187,16 @@ class StartWebSocketServer extends Command
*/ */
public function configurePubSub() public function configurePubSub()
{ {
if (config('websockets.replication.driver', 'local') === 'local') { $this->laravel->singleton(ReplicationInterface::class, function () {
$this->laravel->singleton(ReplicationInterface::class, function () { $driver = config('websockets.replication.driver', 'local');
return new LocalClient;
});
}
if (config('websockets.replication.driver', 'local') === 'redis') { $client = config(
$this->laravel->singleton(ReplicationInterface::class, function () { "websockets.replication.{$driver}.client",
return (new RedisClient)->boot($this->loop); \BeyondCode\LaravelWebSockets\PubSub\Drivers\LocalClient::class
}); );
}
return (new $client)->boot($this->loop);
});
$this->laravel $this->laravel
->get(ReplicationInterface::class) ->get(ReplicationInterface::class)

View File

@ -258,19 +258,18 @@ abstract class TestCase extends BaseTestCase
{ {
// Replace the publish and subscribe clients with a Mocked // Replace the publish and subscribe clients with a Mocked
// factory lazy instance on boot. // factory lazy instance on boot.
if (config('websockets.replication.driver') === 'redis') { $this->app->singleton(ReplicationInterface::class, function () {
$this->app->singleton(ReplicationInterface::class, function () { $driver = config('websockets.replication.driver', 'local');
return (new RedisClient)->boot(
LoopFactory::create(), Mocks\RedisFactory::class
);
});
}
if (config('websockets.replication.driver') === 'local') { $client = config(
$this->app->singleton(ReplicationInterface::class, function () { "websockets.replication.{$driver}.client",
return new LocalClient; \BeyondCode\LaravelWebSockets\PubSub\Drivers\LocalClient::class
}); );
}
return (new $client)->boot(
LoopFactory::create(), Mocks\RedisFactory::class
);
});
} }
protected function runOnlyOnRedisReplication() protected function runOnlyOnRedisReplication()