@@ -56,7 +56,7 @@ class RedisChannelManager extends LocalChannelManager
56
56
*/
57
57
public function __construct (LoopInterface $ loop , $ factoryClass = null )
58
58
{
59
- parent ::construct ($ loop , $ factoryClass );
59
+ parent ::__construct ($ loop , $ factoryClass );
60
60
61
61
$ this ->loop = $ loop ;
62
62
@@ -87,7 +87,7 @@ public function __construct(LoopInterface $loop, $factoryClass = null)
87
87
public function getGlobalChannels ($ appId ): PromiseInterface
88
88
{
89
89
return $ this ->publishClient ->smembers (
90
- $ this ->getRedisKey ($ appId, null , [ ' channels ' ] )
90
+ $ this ->getChannelsRedisHash ($ appId )
91
91
);
92
92
}
93
93
@@ -214,7 +214,7 @@ public function unsubscribeFromApp($appId): PromiseInterface
214
214
public function getGlobalConnectionsCount ($ appId , string $ channelName = null ): PromiseInterface
215
215
{
216
216
return $ this ->publishClient
217
- ->hget ($ this ->getRedisKey ($ appId , $ channelName, [ ' stats ' ] ), 'connections ' )
217
+ ->hget ($ this ->getStatsRedisHash ($ appId , $ channelName ), 'connections ' )
218
218
->then (function ($ count ) {
219
219
return is_null ($ count ) ? 0 : (int ) $ count ;
220
220
});
@@ -237,7 +237,7 @@ public function broadcastAcrossServers($appId, ?string $socketId, string $channe
237
237
$ payload ->serverId = $ serverId ?: $ this ->getServerId ();
238
238
239
239
return $ this ->publishClient
240
- ->publish ($ this ->getRedisKey ($ appId , $ channel ), json_encode ($ payload ))
240
+ ->publish ($ this ->getRedisTopicName ($ appId , $ channel ), json_encode ($ payload ))
241
241
->then (function () use ($ appId , $ socketId , $ channel , $ payload , $ serverId ) {
242
242
return parent ::broadcastAcrossServers ($ appId , $ socketId , $ channel , $ payload , $ serverId );
243
243
});
@@ -293,7 +293,7 @@ public function userLeftPresenceChannel(ConnectionInterface $connection, stdClas
293
293
public function getChannelMembers ($ appId , string $ channel ): PromiseInterface
294
294
{
295
295
return $ this ->publishClient
296
- ->hgetall ($ this ->getRedisKey ($ appId , $ channel, [ ' users ' ] ))
296
+ ->hgetall ($ this ->getUsersRedisHash ($ appId , $ channel ))
297
297
->then (function ($ list ) {
298
298
return collect (Helpers::redisListToArray ($ list ))->map (function ($ user ) {
299
299
return json_decode ($ user );
@@ -311,7 +311,7 @@ public function getChannelMembers($appId, string $channel): PromiseInterface
311
311
public function getChannelMember (ConnectionInterface $ connection , string $ channel ): PromiseInterface
312
312
{
313
313
return $ this ->publishClient ->hget (
314
- $ this ->getRedisKey ($ connection ->app ->id , $ channel, [ ' users ' ] ), $ connection ->socketId
314
+ $ this ->getUsersRedisHash ($ connection ->app ->id , $ channel ), $ connection ->socketId
315
315
);
316
316
}
317
317
@@ -328,7 +328,7 @@ public function getChannelsMembersCount($appId, array $channelNames): PromiseInt
328
328
329
329
foreach ($ channelNames as $ channel ) {
330
330
$ this ->publishClient ->hlen (
331
- $ this ->getRedisKey ($ appId , $ channel, [ ' users ' ] )
331
+ $ this ->getUsersRedisHash ($ appId , $ channel )
332
332
);
333
333
}
334
334
@@ -349,7 +349,7 @@ public function getChannelsMembersCount($appId, array $channelNames): PromiseInt
349
349
public function getMemberSockets ($ userId , $ appId , $ channelName ): PromiseInterface
350
350
{
351
351
return $ this ->publishClient ->smembers (
352
- $ this ->getRedisKey ($ appId , $ channelName , [ $ userId, ' userSockets ' ] )
352
+ $ this ->getUserSocketsRedisHash ($ appId , $ channelName , $ userId )
353
353
);
354
354
}
355
355
@@ -498,7 +498,7 @@ public function getRedisClient()
498
498
public function incrementSubscriptionsCount ($ appId , string $ channel = null , int $ increment = 1 ): PromiseInterface
499
499
{
500
500
return $ this ->publishClient ->hincrby (
501
- $ this ->getRedisKey ($ appId , $ channel, [ ' stats ' ] ), 'connections ' , $ increment
501
+ $ this ->getStatsRedisHash ($ appId , $ channel ), 'connections ' , $ increment
502
502
);
503
503
}
504
504
@@ -527,7 +527,7 @@ public function addConnectionToSet(ConnectionInterface $connection, $moment = nu
527
527
$ moment = $ moment ? Carbon::parse ($ moment ) : Carbon::now ();
528
528
529
529
return $ this ->publishClient ->zadd (
530
- $ this ->getRedisKey ( null , null , [ ' sockets ' ] ),
530
+ $ this ->getSocketsRedisHash ( ),
531
531
$ moment ->format ('U ' ), "{$ connection ->app ->id }: {$ connection ->socketId }"
532
532
);
533
533
}
@@ -541,7 +541,7 @@ public function addConnectionToSet(ConnectionInterface $connection, $moment = nu
541
541
public function removeConnectionFromSet (ConnectionInterface $ connection ): PromiseInterface
542
542
{
543
543
return $ this ->publishClient ->zrem (
544
- $ this ->getRedisKey ( null , null , [ ' sockets ' ] ),
544
+ $ this ->getSocketsRedisHash ( ),
545
545
"{$ connection ->app ->id }: {$ connection ->socketId }"
546
546
);
547
547
}
@@ -563,7 +563,7 @@ public function getConnectionsFromSet(int $start = 0, int $stop = 0, bool $stric
563
563
}
564
564
565
565
return $ this ->publishClient
566
- ->zrangebyscore ($ this ->getRedisKey ( null , null , [ ' sockets ' ] ), $ start , $ stop )
566
+ ->zrangebyscore ($ this ->getSocketsRedisHash ( ), $ start , $ stop )
567
567
->then (function ($ list ) {
568
568
return collect ($ list )->mapWithKeys (function ($ appWithSocket ) {
569
569
[$ appId , $ socketId ] = explode (': ' , $ appWithSocket );
@@ -583,7 +583,7 @@ public function getConnectionsFromSet(int $start = 0, int $stop = 0, bool $stric
583
583
public function addChannelToSet ($ appId , string $ channel ): PromiseInterface
584
584
{
585
585
return $ this ->publishClient ->sadd (
586
- $ this ->getRedisKey ($ appId, null , [ ' channels ' ] ), $ channel
586
+ $ this ->getChannelsRedisHash ($ appId ), $ channel
587
587
);
588
588
}
589
589
@@ -597,7 +597,7 @@ public function addChannelToSet($appId, string $channel): PromiseInterface
597
597
public function removeChannelFromSet ($ appId , string $ channel ): PromiseInterface
598
598
{
599
599
return $ this ->publishClient ->srem (
600
- $ this ->getRedisKey ($ appId, null , [ ' channels ' ] ), $ channel
600
+ $ this ->getChannelsRedisHash ($ appId ), $ channel
601
601
);
602
602
}
603
603
@@ -613,7 +613,7 @@ public function removeChannelFromSet($appId, string $channel): PromiseInterface
613
613
public function storeUserData ($ appId , string $ channel = null , string $ key , $ data ): PromiseInterface
614
614
{
615
615
return $ this ->publishClient ->hset (
616
- $ this ->getRedisKey ($ appId , $ channel, [ ' users ' ] ), $ key , $ data
616
+ $ this ->getUsersRedisHash ($ appId , $ channel ), $ key , $ data
617
617
);
618
618
}
619
619
@@ -628,7 +628,7 @@ public function storeUserData($appId, string $channel = null, string $key, $data
628
628
public function removeUserData ($ appId , string $ channel = null , string $ key ): PromiseInterface
629
629
{
630
630
return $ this ->publishClient ->hdel (
631
- $ this ->getRedisKey ($ appId , $ channel, [ ' users ' ] ), $ key
631
+ $ this ->getUsersRedisHash ($ appId , $ channel ), $ key
632
632
);
633
633
}
634
634
@@ -641,7 +641,7 @@ public function removeUserData($appId, string $channel = null, string $key): Pro
641
641
*/
642
642
public function subscribeToTopic ($ appId , string $ channel = null ): PromiseInterface
643
643
{
644
- $ topic = $ this ->getRedisKey ($ appId , $ channel );
644
+ $ topic = $ this ->getRedisTopicName ($ appId , $ channel );
645
645
646
646
DashboardLogger::log ($ appId , DashboardLogger::TYPE_REPLICATOR_SUBSCRIBED , [
647
647
'serverId ' => $ this ->getServerId (),
@@ -660,7 +660,7 @@ public function subscribeToTopic($appId, string $channel = null): PromiseInterfa
660
660
*/
661
661
public function unsubscribeFromTopic ($ appId , string $ channel = null ): PromiseInterface
662
662
{
663
- $ topic = $ this ->getRedisKey ($ appId , $ channel );
663
+ $ topic = $ this ->getRedisTopicName ($ appId , $ channel );
664
664
665
665
DashboardLogger::log ($ appId , DashboardLogger::TYPE_REPLICATOR_UNSUBSCRIBED , [
666
666
'serverId ' => $ this ->getServerId (),
@@ -682,7 +682,7 @@ public function unsubscribeFromTopic($appId, string $channel = null): PromiseInt
682
682
protected function addUserSocket ($ appId , string $ channel , stdClass $ user , string $ socketId ): PromiseInterface
683
683
{
684
684
return $ this ->publishClient ->sadd (
685
- $ this ->getRedisKey ($ appId , $ channel , [ $ user ->user_id , ' userSockets ' ] ), $ socketId
685
+ $ this ->getUserSocketsRedisHash ($ appId , $ channel , $ user ->user_id ), $ socketId
686
686
);
687
687
}
688
688
@@ -698,7 +698,7 @@ protected function addUserSocket($appId, string $channel, stdClass $user, string
698
698
protected function removeUserSocket ($ appId , string $ channel , stdClass $ user , string $ socketId ): PromiseInterface
699
699
{
700
700
return $ this ->publishClient ->srem (
701
- $ this ->getRedisKey ($ appId , $ channel , [ $ user ->user_id , ' userSockets ' ] ), $ socketId
701
+ $ this ->getUserSocketsRedisHash ($ appId , $ channel , $ user ->user_id ), $ socketId
702
702
);
703
703
}
704
704
@@ -729,6 +729,79 @@ public function getRedisKey($appId = null, string $channel = null, array $suffix
729
729
return $ hash ;
730
730
}
731
731
732
+ /**
733
+ * Get the statistics Redis hash.
734
+ *
735
+ * @param string|int $appId
736
+ * @param string|null $channel
737
+ * @return string
738
+ */
739
+ public function getStatsRedisHash ($ appId , string $ channel = null ): string
740
+ {
741
+ return $ this ->getRedisKey ($ appId , $ channel , ['stats ' ]);
742
+ }
743
+
744
+ /**
745
+ * Get the sockets Redis hash used to store all sockets ids.
746
+ *
747
+ * @return string
748
+ */
749
+ public function getSocketsRedisHash (): string
750
+ {
751
+ return $ this ->getRedisKey (null , null , ['sockets ' ]);
752
+ }
753
+
754
+ /**
755
+ * Get the channels Redis hash for a specific app id, used
756
+ * to store existing channels.
757
+ *
758
+ * @param string|int $appId
759
+ * @return string
760
+ */
761
+ public function getChannelsRedisHash ($ appId ): string
762
+ {
763
+ return $ this ->getRedisKey ($ appId , null , ['channels ' ]);
764
+ }
765
+
766
+ /**
767
+ * Get the Redis hash for storing presence channels users.
768
+ *
769
+ * @param string|int $appId
770
+ * @param string|null $channel
771
+ * @return string
772
+ */
773
+ public function getUsersRedisHash ($ appId , string $ channel = null ): string
774
+ {
775
+ return $ this ->getRedisKey ($ appId , $ channel , ['users ' ]);
776
+ }
777
+
778
+ /**
779
+ * Get the Redis hash for storing socket ids
780
+ * for a specific presence channels user.
781
+ *
782
+ * @param string|int $appId
783
+ * @param string|null $channel
784
+ * @param string|int|null $userId
785
+ * @return string
786
+ */
787
+ public function getUserSocketsRedisHash ($ appId , string $ channel = null , $ userId = null ): string
788
+ {
789
+ return $ this ->getRedisKey ($ appId , $ channel , [$ userId , 'userSockets ' ]);
790
+ }
791
+
792
+ /**
793
+ * Get the Redis topic name for PubSub
794
+ * used to transfer info between servers.
795
+ *
796
+ * @param string|int $appId
797
+ * @param string|null $channel
798
+ * @return string
799
+ */
800
+ public function getRedisTopicName ($ appId , string $ channel = null ): string
801
+ {
802
+ return $ this ->getRedisKey ($ appId , $ channel );
803
+ }
804
+
732
805
/**
733
806
* Get a new RedisLock instance to avoid race conditions.
734
807
*
0 commit comments