Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
64b0fa8
Removed the setupReplication trait
rennokki Aug 13, 2020
51f84e3
set up tests
rennokki Aug 13, 2020
d7038ed
Added Redis setup at run
rennokki Aug 13, 2020
64d11c4
Added redis as service
rennokki Aug 13, 2020
1446cf8
Running redis driver tests only on linux
rennokki Aug 13, 2020
4389fd1
Added soft default to replication driver check
rennokki Aug 13, 2020
0ebf223
Renamed the prop to replicator
rennokki Aug 13, 2020
7053600
Merge branch 'refactor/tests' of github.com:beyondcode/laravel-websoc…
rennokki Aug 13, 2020
b1d29d0
swap to xdebug
rennokki Aug 13, 2020
00b3edf
Added Illuminate\Broadcasting\BroadcastServiceProvider
rennokki Aug 13, 2020
ca9e90d
Setup redis only on ubuntu-latest
rennokki Aug 13, 2020
8f52393
Using only Redis 6.x
rennokki Aug 13, 2020
14f54da
$this->app->make
rennokki Aug 13, 2020
5838aca
Set up config for broadcasting
rennokki Aug 13, 2020
5997dd4
wip docblocks
rennokki Aug 14, 2020
4c23363
wip dashboard logger
rennokki Aug 14, 2020
7458c3e
Emptied tests for replication
rennokki Aug 14, 2020
22fcddb
docblocks
rennokki Aug 14, 2020
4c64493
Improved loggin display
rennokki Aug 14, 2020
25694c7
wip
rennokki Aug 14, 2020
5c3e87e
Apply fixes from StyleCI (#461)
rennokki Aug 14, 2020
50f0b70
wip
rennokki Aug 14, 2020
0fdc0bc
Merge branch 'refactor/pubsub-replication-tests' of github.com:beyond…
rennokki Aug 14, 2020
939ae17
Apply fixes from StyleCI (#462)
rennokki Aug 14, 2020
04cc2e7
missing class
rennokki Aug 14, 2020
4ae3d81
assert publish
rennokki Aug 14, 2020
92dd8f4
skipped some tests until further addo
rennokki Aug 14, 2020
b140d1f
Updated command run for windows envs
rennokki Aug 14, 2020
c543bbc
Updated command
rennokki Aug 14, 2020
b7a00ba
wip incomplete tests
rennokki Aug 14, 2020
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
17 changes: 14 additions & 3 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,25 @@ jobs:
os: [ubuntu-latest, windows-latest]
php: [7.4, 7.3, 7.2]
laravel: [6.*, 7.*]
redis: [5, 6]
dependency-version: [prefer-lowest, prefer-stable]
include:
- laravel: 7.*
testbench: 5.*
- laravel: 6.*
testbench: 4.*

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ${{ matrix.os }}
name: P${{ matrix.php }} - L${{ matrix.laravel }} - R${{ matrix.redis }} - ${{ matrix.dependency-version }} - ${{ matrix.os }}

steps:
- name: Checkout code
uses: actions/checkout@v1

- name: Setup Redis ${{ matrix.redis }}
uses: supercharge/redis-github-action@1.1.0
with:
redis-version: ${{ matrix.redis }}

- name: Cache dependencies
uses: actions/cache@v1
with:
Expand All @@ -42,9 +48,14 @@ jobs:
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest

- name: Execute tests
run: vendor/bin/phpunit --coverage-text --coverage-clover=coverage.xml
- name: Execute tests with Local driver
run: REPLICATION_DRIVER=local phpunit --coverage-text --coverage-clover=coverage_local.xml

- name: Execute tests with Redis driver
run: REPLICATION_DRIVER=redis phpunit --coverage-text --coverage-clover=coverage_redis.xml
if: ${{ matrix.os == 'ubuntu-latest' }}

- uses: codecov/codecov-action@v1
with:
fail_ci_if_error: false
file: '*.xml'
12 changes: 5 additions & 7 deletions config/websockets.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,23 +143,21 @@

/*
|--------------------------------------------------------------------------
| Broadcasting Replication
| Broadcasting Replication PubSub
|--------------------------------------------------------------------------
|
| You can enable replication to publish and subscribe to
| messages across the driver.
|
| By default, it is disabled, but you can configure it to use drivers

| By default, it is set to 'local', but you can configure it to use drivers
| like Redis to ensure connection between multiple instances of
| WebSocket servers.
| WebSocket servers. Just set the driver to 'redis' to enable the PubSub using Redis.
|
*/

'replication' => [

'enabled' => false,

'driver' => 'redis',
'driver' => 'local',

'redis' => [

Expand Down
4 changes: 3 additions & 1 deletion src/Console/StartWebSocketServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ protected function startWebSocketServer()

protected function configurePubSubReplication()
{
$this->laravel->get(ReplicationInterface::class)->boot($this->loop);
$this->laravel
->get(ReplicationInterface::class)
->boot($this->loop);

return $this;
}
Expand Down
8 changes: 4 additions & 4 deletions src/HttpApi/Controllers/FetchChannelsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
class FetchChannelsController extends Controller
{
/** @var ReplicationInterface */
protected $replication;
protected $pubsub;

public function __construct(ChannelManager $channelManager, ReplicationInterface $replication)
public function __construct(ChannelManager $channelManager, ReplicationInterface $pubsub)
{
parent::__construct($channelManager);

$this->replication = $replication;
$this->pubsub = $pubsub;
}

public function __invoke(Request $request)
Expand Down Expand Up @@ -51,7 +51,7 @@ public function __invoke(Request $request)

// We ask the replication backend to get us the member count per channel.
// We get $counts back as a key-value array of channel names and their member count.
return $this->replication
return $this->pubsub
->channelMemberCounts($request->appId, $channelNames)
->then(function (array $counts) use ($channels, $attributes) {
return [
Expand Down
1 change: 0 additions & 1 deletion src/PubSub/Drivers/LocalClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public function boot(LoopInterface $loop): ReplicationInterface
*/
public function publish(string $appId, string $channel, stdClass $payload): bool
{
// Nothing to do, nobody to publish to
return true;
}

Expand Down
12 changes: 8 additions & 4 deletions src/PubSub/Drivers/RedisClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,20 +257,24 @@ public function channelMemberCounts(string $appId, array $channelNames): Promise
*/
protected function getConnectionUri()
{
$name = config('websockets.replication.connection') ?? 'default';
$config = config("database.redis.$name");
$name = config('websockets.replication.redis.connection') ?? 'default';
$config = config('database.redis')[$name];

$host = $config['host'];
$port = $config['port'] ? (':'.$config['port']) : ':6379';
$port = $config['port'] ?: 6379;

$query = [];

if ($config['password']) {
$query['password'] = $config['password'];
}

if ($config['database']) {
$query['database'] = $config['database'];
}

$query = http_build_query($query);

return "redis://$host$port".($query ? '?'.$query : '');
return "redis://{$host}:{$port}".($query ? "?{$query}" : '');
}
}
10 changes: 5 additions & 5 deletions src/WebSockets/Channels/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ class Channel
protected $channelName;

/** @var ReplicationInterface */
protected $replication;
protected $pubsub;

/** @var \Ratchet\ConnectionInterface[] */
protected $subscribedConnections = [];

public function __construct(string $channelName)
{
$this->channelName = $channelName;
$this->replication = app(ReplicationInterface::class);
$this->pubsub = app(ReplicationInterface::class);
}

public function getChannelName(): string
Expand Down Expand Up @@ -68,7 +68,7 @@ public function subscribe(ConnectionInterface $connection, stdClass $payload)
$this->saveConnection($connection);

// Subscribe to broadcasted messages from the pub/sub backend
$this->replication->subscribe($connection->app->id, $this->channelName);
$this->pubsub->subscribe($connection->app->id, $this->channelName);

$connection->send(json_encode([
'event' => 'pusher_internal:subscription_succeeded',
Expand All @@ -81,7 +81,7 @@ public function unsubscribe(ConnectionInterface $connection)
unset($this->subscribedConnections[$connection->socketId]);

// Unsubscribe from the pub/sub backend
$this->replication->unsubscribe($connection->app->id, $this->channelName);
$this->pubsub->unsubscribe($connection->app->id, $this->channelName);

if (! $this->hasConnections()) {
DashboardLogger::vacated($connection, $this->channelName);
Expand Down Expand Up @@ -120,7 +120,7 @@ public function broadcastToEveryoneExcept($payload, ?string $socketId, string $a
// in this case. If this came from TriggerEventController, then we still want
// to publish to get the message out to other server instances.
if ($publish) {
$this->replication->publish($appId, $this->channelName, $payload);
$this->pubsub->publish($appId, $this->channelName, $payload);
}

// Performance optimization, if we don't have a socket ID,
Expand Down
10 changes: 5 additions & 5 deletions src/WebSockets/Channels/PresenceChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class PresenceChannel extends Channel
public function getUsers(string $appId)
{
// Get the members list from the replication backend
return $this->replication
return $this->pubsub
->channelMembers($appId, $this->channelName);
}

Expand All @@ -49,7 +49,7 @@ public function subscribe(ConnectionInterface $connection, stdClass $payload)
$this->users[$connection->socketId] = $channelData;

// Add the connection as a member of the channel
$this->replication
$this->pubsub
->joinChannel(
$connection->app->id,
$this->channelName,
Expand All @@ -59,7 +59,7 @@ public function subscribe(ConnectionInterface $connection, stdClass $payload)

// We need to pull the channel data from the replication backend,
// otherwise we won't be sending the full details of the channel
$this->replication
$this->pubsub
->channelMembers($connection->app->id, $this->channelName)
->then(function ($users) use ($connection) {
// Send the success event
Expand All @@ -86,7 +86,7 @@ public function unsubscribe(ConnectionInterface $connection)
}

// Remove the connection as a member of the channel
$this->replication
$this->pubsub
->leaveChannel(
$connection->app->id,
$this->channelName,
Expand All @@ -110,7 +110,7 @@ public function unsubscribe(ConnectionInterface $connection)
*/
public function toArray(string $appId = null)
{
return $this->replication
return $this->pubsub
->channelMembers($appId, $this->channelName)
->then(function ($users) {
return array_merge(parent::toArray(), [
Expand Down
17 changes: 9 additions & 8 deletions src/WebSocketsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Illuminate\Support\ServiceProvider;
use Psr\Log\LoggerInterface;
use Pusher\Pusher;
use React\EventLoop\Factory as LoopFactory;

class WebSocketsServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -56,19 +57,19 @@ public function boot()

protected function configurePubSub()
{
if (config('websockets.replication.enabled') !== true || config('websockets.replication.driver') !== 'redis') {
if (config('websockets.replication.driver') === 'local') {
$this->app->singleton(ReplicationInterface::class, function () {
return new LocalClient();
return new LocalClient;
});

return;
}

$this->app->singleton(ReplicationInterface::class, function () {
return (new RedisClient())->boot($this->loop);
});
if (config('websockets.replication.driver') === 'redis') {
$this->app->singleton(ReplicationInterface::class, function () {
return (new RedisClient)->boot($this->loop ?? LoopFactory::create());
});
}

$this->app->get(BroadcastManager::class)->extend('redis-pusher', function ($app, array $config) {
$this->app->get(BroadcastManager::class)->extend('websockets', function ($app, array $config) {
$pusher = new Pusher(
$config['key'], $config['secret'],
$config['app_id'], $config['options'] ?? []
Expand Down
11 changes: 1 addition & 10 deletions tests/Channels/ChannelReplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,7 @@

namespace BeyondCode\LaravelWebSockets\Tests\Channels;

use BeyondCode\LaravelWebSockets\Tests\TestsReplication;

class ChannelReplicationTest extends ChannelTest
{
use TestsReplication;

public function setUp(): void
{
parent::setUp();

$this->setupReplication();
}
//
}
11 changes: 1 addition & 10 deletions tests/Channels/PresenceChannelReplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,7 @@

namespace BeyondCode\LaravelWebSockets\Tests\Channels;

use BeyondCode\LaravelWebSockets\Tests\TestsReplication;

class PresenceChannelReplicationTest extends PresenceChannelTest
{
use TestsReplication;

public function setUp(): void
{
parent::setUp();

$this->setupReplication();
}
//
}
11 changes: 1 addition & 10 deletions tests/HttpApi/FetchChannelReplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,7 @@

namespace BeyondCode\LaravelWebSockets\Tests\HttpApi;

use BeyondCode\LaravelWebSockets\Tests\TestsReplication;

class FetchChannelReplicationTest extends FetchChannelTest
{
use TestsReplication;

public function setUp(): void
{
parent::setUp();

$this->setupReplication();
}
//
}
11 changes: 1 addition & 10 deletions tests/HttpApi/FetchChannelsReplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,7 @@

namespace BeyondCode\LaravelWebSockets\Tests\HttpApi;

use BeyondCode\LaravelWebSockets\Tests\TestsReplication;

class FetchChannelsReplicationTest extends FetchChannelsTest
{
use TestsReplication;

public function setUp(): void
{
parent::setUp();

$this->setupReplication();
}
//
}
11 changes: 1 addition & 10 deletions tests/HttpApi/FetchUsersReplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,7 @@

namespace BeyondCode\LaravelWebSockets\Tests\HttpApi;

use BeyondCode\LaravelWebSockets\Tests\TestsReplication;

class FetchUsersReplicationTest extends FetchUsersTest
{
use TestsReplication;

public function setUp(): void
{
parent::setUp();

$this->setupReplication();
}
//
}
12 changes: 12 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ protected function getEnvironmentSetUp($app)
],
]);

$app['config']->set('database.redis.default', [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_DB', '0'),
]);

$app['config']->set(
'websockets.replication.driver',
getenv('REPLICATION_DRIVER') ?: 'local'
);

include_once __DIR__.'/../database/migrations/create_websockets_statistics_entries_table.php.stub';

(new \CreateWebSocketsStatisticsEntriesTable())->up();
Expand Down
Loading