Using the built-in Redis cache connection to handle non-pubsub features.

This commit is contained in:
Alex Renoki 2020-09-03 16:50:37 +03:00
parent fadb3fc123
commit d5a90d8440
4 changed files with 51 additions and 9 deletions

View File

@ -226,7 +226,7 @@ class RedisClient extends LocalClient
*/
public function joinChannel($appId, string $channel, string $socketId, string $data)
{
$this->publishClient->__call('hset', [$this->getTopicName($appId, $channel), $socketId, $data]);
$this->redis->hset($this->getTopicName($appId, $channel), $socketId, $data);
DashboardLogger::log($appId, DashboardLogger::TYPE_REPLICATOR_JOINED_CHANNEL, [
'channel' => $channel,
@ -248,7 +248,7 @@ class RedisClient extends LocalClient
*/
public function leaveChannel($appId, string $channel, string $socketId)
{
$this->publishClient->__call('hdel', [$this->getTopicName($appId, $channel), $socketId]);
$this->redis->hdel($this->getTopicName($appId, $channel), $socketId);
DashboardLogger::log($appId, DashboardLogger::TYPE_REPLICATOR_LEFT_CHANNEL, [
'channel' => $channel,

View File

@ -4,9 +4,17 @@ namespace BeyondCode\LaravelWebSockets\Tests\Channels;
use BeyondCode\LaravelWebSockets\Tests\Mocks\Message;
use BeyondCode\LaravelWebSockets\Tests\TestCase;
use Illuminate\Support\Facades\Cache;
class PresenceChannelReplicationTest extends TestCase
{
/**
* The Redis manager instance.
*
* @var \Illuminate\Redis\RedisManager
*/
protected $redis;
/**
* {@inheritdoc}
*/
@ -15,6 +23,8 @@ class PresenceChannelReplicationTest extends TestCase
parent::setUp();
$this->runOnlyOnRedisReplication();
$this->redis = Cache::getRedis();
}
/** @test */
@ -45,13 +55,17 @@ class PresenceChannelReplicationTest extends TestCase
$this->pusherServer->onMessage($connection, $message);
$this->getPublishClient()
->assertCalledWithArgs('hset', [
->assertNotCalledWithArgs('hset', [
'laravel_database_1234:presence-channel',
$connection->socketId,
json_encode($channelData),
])
->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-channel'])
->assertCalled('publish');
$this->assertNotNull(
$this->redis->hget('laravel_database_1234:presence-channel', $connection->socketId)
);
}
/** @test */
@ -82,7 +96,7 @@ class PresenceChannelReplicationTest extends TestCase
->assertEventDispatched('message');
$this->getPublishClient()
->assertCalled('hset')
->assertNotCalled('hset')
->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-channel'])
->assertCalled('publish');
@ -100,7 +114,7 @@ class PresenceChannelReplicationTest extends TestCase
$this->pusherServer->onMessage($connection, $message);
$this->getPublishClient()
->assertCalled('hdel')
->assertNotCalled('hdel')
->assertCalled('publish');
}
@ -129,7 +143,7 @@ class PresenceChannelReplicationTest extends TestCase
$this->pusherServer->onMessage($connection, $message);
$this->getPublishClient()
->assertCalled('hset')
->assertNotCalled('hset')
->assertcalledWithArgs('hgetall', ['laravel_database_1234:presence-channel'])
->assertCalled('publish');
}

View File

@ -48,7 +48,7 @@ class FetchChannelsReplicationTest extends TestCase
->assertEventDispatched('message');
$this->getPublishClient()
->assertCalled('hset')
->assertNotCalled('hset')
->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-channel'])
->assertCalled('publish')
->assertCalled('multi')
@ -88,7 +88,7 @@ class FetchChannelsReplicationTest extends TestCase
->assertEventDispatched('message');
$this->getPublishClient()
->assertCalled('hset')
->assertNotCalled('hset')
->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-global.1'])
->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-global.2'])
->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-notglobal.2'])
@ -133,7 +133,7 @@ class FetchChannelsReplicationTest extends TestCase
->assertEventDispatched('message');
$this->getPublishClient()
->assertCalled('hset')
->assertNotCalled('hset')
->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-global.1'])
->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-global.2'])
->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-notglobal.2'])

View File

@ -8,6 +8,7 @@ use BeyondCode\LaravelWebSockets\Statistics\Logger\NullStatisticsLogger;
use BeyondCode\LaravelWebSockets\Statistics\Logger\RedisStatisticsLogger;
use BeyondCode\LaravelWebSockets\Statistics\Models\WebSocketsStatisticsEntry;
use BeyondCode\LaravelWebSockets\Tests\TestCase;
use Illuminate\Support\Facades\Cache;
class StatisticsLoggerTest extends TestCase
{
@ -32,6 +33,33 @@ class StatisticsLoggerTest extends TestCase
/** @test */
public function it_counts_unique_connections_no_channel_subscriptions()
{
$this->runOnlyOnLocalReplication();
$connections = [];
$connections[] = $this->getConnectedWebSocketConnection(['channel-1', 'channel-2']);
$connections[] = $this->getConnectedWebSocketConnection(['channel-1', 'channel-2']);
$connections[] = $this->getConnectedWebSocketConnection(['channel-1']);
$this->assertEquals(3, StatisticsLogger::getForAppId(1234)['peak_connection_count']);
$this->pusherServer->onClose(array_pop($connections));
$this->pusherServer->onClose(array_pop($connections));
StatisticsLogger::save();
$this->assertEquals(1, StatisticsLogger::getForAppId(1234)['peak_connection_count']);
}
/** @test */
public function it_counts_unique_connections_no_channel_subscriptions_on_redis()
{
$this->runOnlyOnRedisReplication();
$redis = Cache::getRedis();
$redis->hdel('laravel_database_1234', 'connections');
$connections = [];
$connections[] = $this->getConnectedWebSocketConnection(['channel-1', 'channel-2']);