Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit 5b6bdf4

Browse files
committed
Added configurable client for each replication driver.
1 parent 00faff7 commit 5b6bdf4

File tree

3 files changed

+52
-25
lines changed

3 files changed

+52
-25
lines changed

config/websockets.php

+31
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,41 @@
166166

167167
'driver' => env('LARAVEL_WEBSOCKETS_REPLICATION_DRIVER', 'local'),
168168

169+
/*
170+
|--------------------------------------------------------------------------
171+
| Local Replication
172+
|--------------------------------------------------------------------------
173+
|
174+
| Local replication is actually a null replicator, meaning that it
175+
| is the default behaviour of storing the connections into an array.
176+
|
177+
*/
178+
179+
'local' => [
180+
181+
'client' => \BeyondCode\LaravelWebSockets\PubSub\Drivers\LocalClient::class,
182+
183+
],
184+
185+
/*
186+
|--------------------------------------------------------------------------
187+
| Redis Replication
188+
|--------------------------------------------------------------------------
189+
|
190+
| Redis replication relies on the Redis' Pub/Sub protocol. When users
191+
| are connected across multiple nodes, whenever some event gets triggered
192+
| on one instance, the rest of the instances get the same copy and, in
193+
| case the connected users to other instances are valid to receive
194+
| the event, they will receive it.
195+
|
196+
*/
197+
169198
'redis' => [
170199

171200
'connection' => env('LARAVEL_WEBSOCKETS_REPLICATION_CONNECTION', 'default'),
172201

202+
'client' => \BeyondCode\LaravelWebSockets\PubSub\Drivers\RedisClient::class,
203+
173204
],
174205

175206
],

src/Console/StartWebSocketServer.php

+9-12
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
use BeyondCode\LaravelWebSockets\Facades\StatisticsLogger;
66
use BeyondCode\LaravelWebSockets\Facades\WebSocketsRouter;
7-
use BeyondCode\LaravelWebSockets\PubSub\Drivers\LocalClient;
8-
use BeyondCode\LaravelWebSockets\PubSub\Drivers\RedisClient;
97
use BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface;
108
use BeyondCode\LaravelWebSockets\Server\Logger\ConnectionLogger;
119
use BeyondCode\LaravelWebSockets\Server\Logger\HttpLogger;
@@ -189,17 +187,16 @@ public function configureRestartTimer()
189187
*/
190188
public function configurePubSub()
191189
{
192-
if (config('websockets.replication.driver', 'local') === 'local') {
193-
$this->laravel->singleton(ReplicationInterface::class, function () {
194-
return new LocalClient;
195-
});
196-
}
190+
$this->laravel->singleton(ReplicationInterface::class, function () {
191+
$driver = config('websockets.replication.driver', 'local');
197192

198-
if (config('websockets.replication.driver', 'local') === 'redis') {
199-
$this->laravel->singleton(ReplicationInterface::class, function () {
200-
return (new RedisClient)->boot($this->loop);
201-
});
202-
}
193+
$client = config(
194+
"websockets.replication.{$driver}.client",
195+
\BeyondCode\LaravelWebSockets\PubSub\Drivers\LocalClient::class
196+
);
197+
198+
return (new $client)->boot($this->loop);
199+
});
203200

204201
$this->laravel
205202
->get(ReplicationInterface::class)

tests/TestCase.php

+12-13
Original file line numberDiff line numberDiff line change
@@ -258,19 +258,18 @@ protected function configurePubSub()
258258
{
259259
// Replace the publish and subscribe clients with a Mocked
260260
// factory lazy instance on boot.
261-
if (config('websockets.replication.driver') === 'redis') {
262-
$this->app->singleton(ReplicationInterface::class, function () {
263-
return (new RedisClient)->boot(
264-
LoopFactory::create(), Mocks\RedisFactory::class
265-
);
266-
});
267-
}
268-
269-
if (config('websockets.replication.driver') === 'local') {
270-
$this->app->singleton(ReplicationInterface::class, function () {
271-
return new LocalClient;
272-
});
273-
}
261+
$this->app->singleton(ReplicationInterface::class, function () {
262+
$driver = config('websockets.replication.driver', 'local');
263+
264+
$client = config(
265+
"websockets.replication.{$driver}.client",
266+
\BeyondCode\LaravelWebSockets\PubSub\Drivers\LocalClient::class
267+
);
268+
269+
return (new $client)->boot(
270+
LoopFactory::create(), Mocks\RedisFactory::class
271+
);
272+
});
274273
}
275274

276275
protected function runOnlyOnRedisReplication()

0 commit comments

Comments
 (0)