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

Commit d5a90d8

Browse files
committed
Using the built-in Redis cache connection to handle non-pubsub features.
1 parent fadb3fc commit d5a90d8

File tree

4 files changed

+51
-9
lines changed

4 files changed

+51
-9
lines changed

src/PubSub/Drivers/RedisClient.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ public function unsubscribeFromApp($appId): bool
226226
*/
227227
public function joinChannel($appId, string $channel, string $socketId, string $data)
228228
{
229-
$this->publishClient->__call('hset', [$this->getTopicName($appId, $channel), $socketId, $data]);
229+
$this->redis->hset($this->getTopicName($appId, $channel), $socketId, $data);
230230

231231
DashboardLogger::log($appId, DashboardLogger::TYPE_REPLICATOR_JOINED_CHANNEL, [
232232
'channel' => $channel,
@@ -248,7 +248,7 @@ public function joinChannel($appId, string $channel, string $socketId, string $d
248248
*/
249249
public function leaveChannel($appId, string $channel, string $socketId)
250250
{
251-
$this->publishClient->__call('hdel', [$this->getTopicName($appId, $channel), $socketId]);
251+
$this->redis->hdel($this->getTopicName($appId, $channel), $socketId);
252252

253253
DashboardLogger::log($appId, DashboardLogger::TYPE_REPLICATOR_LEFT_CHANNEL, [
254254
'channel' => $channel,

tests/Channels/PresenceChannelReplicationTest.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,17 @@
44

55
use BeyondCode\LaravelWebSockets\Tests\Mocks\Message;
66
use BeyondCode\LaravelWebSockets\Tests\TestCase;
7+
use Illuminate\Support\Facades\Cache;
78

89
class PresenceChannelReplicationTest extends TestCase
910
{
11+
/**
12+
* The Redis manager instance.
13+
*
14+
* @var \Illuminate\Redis\RedisManager
15+
*/
16+
protected $redis;
17+
1018
/**
1119
* {@inheritdoc}
1220
*/
@@ -15,6 +23,8 @@ public function setUp(): void
1523
parent::setUp();
1624

1725
$this->runOnlyOnRedisReplication();
26+
27+
$this->redis = Cache::getRedis();
1828
}
1929

2030
/** @test */
@@ -45,13 +55,17 @@ public function clients_with_valid_auth_signatures_can_join_presence_channels()
4555
$this->pusherServer->onMessage($connection, $message);
4656

4757
$this->getPublishClient()
48-
->assertCalledWithArgs('hset', [
58+
->assertNotCalledWithArgs('hset', [
4959
'laravel_database_1234:presence-channel',
5060
$connection->socketId,
5161
json_encode($channelData),
5262
])
5363
->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-channel'])
5464
->assertCalled('publish');
65+
66+
$this->assertNotNull(
67+
$this->redis->hget('laravel_database_1234:presence-channel', $connection->socketId)
68+
);
5569
}
5670

5771
/** @test */
@@ -82,7 +96,7 @@ public function clients_with_valid_auth_signatures_can_leave_presence_channels()
8296
->assertEventDispatched('message');
8397

8498
$this->getPublishClient()
85-
->assertCalled('hset')
99+
->assertNotCalled('hset')
86100
->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-channel'])
87101
->assertCalled('publish');
88102

@@ -100,7 +114,7 @@ public function clients_with_valid_auth_signatures_can_leave_presence_channels()
100114
$this->pusherServer->onMessage($connection, $message);
101115

102116
$this->getPublishClient()
103-
->assertCalled('hdel')
117+
->assertNotCalled('hdel')
104118
->assertCalled('publish');
105119
}
106120

@@ -129,7 +143,7 @@ public function clients_with_no_user_info_can_join_presence_channels()
129143
$this->pusherServer->onMessage($connection, $message);
130144

131145
$this->getPublishClient()
132-
->assertCalled('hset')
146+
->assertNotCalled('hset')
133147
->assertcalledWithArgs('hgetall', ['laravel_database_1234:presence-channel'])
134148
->assertCalled('publish');
135149
}

tests/HttpApi/FetchChannelsReplicationTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function replication_it_returns_the_channel_information()
4848
->assertEventDispatched('message');
4949

5050
$this->getPublishClient()
51-
->assertCalled('hset')
51+
->assertNotCalled('hset')
5252
->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-channel'])
5353
->assertCalled('publish')
5454
->assertCalled('multi')
@@ -88,7 +88,7 @@ public function replication_it_returns_the_channel_information_for_prefix()
8888
->assertEventDispatched('message');
8989

9090
$this->getPublishClient()
91-
->assertCalled('hset')
91+
->assertNotCalled('hset')
9292
->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-global.1'])
9393
->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-global.2'])
9494
->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-notglobal.2'])
@@ -133,7 +133,7 @@ public function replication_it_returns_the_channel_information_for_prefix_with_u
133133
->assertEventDispatched('message');
134134

135135
$this->getPublishClient()
136-
->assertCalled('hset')
136+
->assertNotCalled('hset')
137137
->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-global.1'])
138138
->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-global.2'])
139139
->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-notglobal.2'])

tests/Statistics/Logger/StatisticsLoggerTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use BeyondCode\LaravelWebSockets\Statistics\Logger\RedisStatisticsLogger;
99
use BeyondCode\LaravelWebSockets\Statistics\Models\WebSocketsStatisticsEntry;
1010
use BeyondCode\LaravelWebSockets\Tests\TestCase;
11+
use Illuminate\Support\Facades\Cache;
1112

1213
class StatisticsLoggerTest extends TestCase
1314
{
@@ -32,6 +33,33 @@ public function it_counts_connections()
3233
/** @test */
3334
public function it_counts_unique_connections_no_channel_subscriptions()
3435
{
36+
$this->runOnlyOnLocalReplication();
37+
38+
$connections = [];
39+
40+
$connections[] = $this->getConnectedWebSocketConnection(['channel-1', 'channel-2']);
41+
$connections[] = $this->getConnectedWebSocketConnection(['channel-1', 'channel-2']);
42+
$connections[] = $this->getConnectedWebSocketConnection(['channel-1']);
43+
44+
$this->assertEquals(3, StatisticsLogger::getForAppId(1234)['peak_connection_count']);
45+
46+
$this->pusherServer->onClose(array_pop($connections));
47+
$this->pusherServer->onClose(array_pop($connections));
48+
49+
StatisticsLogger::save();
50+
51+
$this->assertEquals(1, StatisticsLogger::getForAppId(1234)['peak_connection_count']);
52+
}
53+
54+
/** @test */
55+
public function it_counts_unique_connections_no_channel_subscriptions_on_redis()
56+
{
57+
$this->runOnlyOnRedisReplication();
58+
59+
$redis = Cache::getRedis();
60+
61+
$redis->hdel('laravel_database_1234', 'connections');
62+
3563
$connections = [];
3664

3765
$connections[] = $this->getConnectedWebSocketConnection(['channel-1', 'channel-2']);

0 commit comments

Comments
 (0)