15
15
class RedisClient implements ReplicationInterface
16
16
{
17
17
/**
18
+ * The running loop.
19
+ *
18
20
* @var LoopInterface
19
21
*/
20
22
protected $ loop ;
21
23
22
24
/**
25
+ * The unique server identifier.
26
+ *
23
27
* @var string
24
28
*/
25
29
protected $ serverId ;
26
30
27
31
/**
32
+ * The pub client.
33
+ *
28
34
* @var Client
29
35
*/
30
36
protected $ publishClient ;
31
37
32
38
/**
39
+ * The sub client.
40
+ *
33
41
* @var Client
34
42
*/
35
43
protected $ subscribeClient ;
@@ -45,7 +53,9 @@ class RedisClient implements ReplicationInterface
45
53
protected $ subscribedChannels = [];
46
54
47
55
/**
48
- * RedisClient constructor.
56
+ * Create a new Redis client.
57
+ *
58
+ * @return void
49
59
*/
50
60
public function __construct ()
51
61
{
@@ -68,6 +78,7 @@ public function boot(LoopInterface $loop): ReplicationInterface
68
78
$ this ->publishClient = $ factory ->createLazyClient ($ connectionUri );
69
79
$ this ->subscribeClient = $ factory ->createLazyClient ($ connectionUri );
70
80
81
+ // The subscribed client gets a message, it triggers the onMessage().
71
82
$ this ->subscribeClient ->on ('message ' , function ($ channel , $ payload ) {
72
83
$ this ->onMessage ($ channel , $ payload );
73
84
});
@@ -86,7 +97,7 @@ protected function onMessage(string $redisChannel, string $payload)
86
97
{
87
98
$ payload = json_decode ($ payload );
88
99
89
- // Ignore messages sent by ourselves
100
+ // Ignore messages sent by ourselves.
90
101
if (isset ($ payload ->serverId ) && $ this ->serverId === $ payload ->serverId ) {
91
102
return ;
92
103
}
@@ -99,10 +110,9 @@ protected function onMessage(string $redisChannel, string $payload)
99
110
// expect the channel name to not include the app ID.
100
111
$ payload ->channel = Str::after ($ redisChannel , "$ appId: " );
101
112
102
- /* @var ChannelManager $channelManager */
103
113
$ channelManager = app (ChannelManager::class);
104
114
105
- // Load the Channel instance, if any
115
+ // Load the Channel instance to sync.
106
116
$ channel = $ channelManager ->find ($ appId , $ payload ->channel );
107
117
108
118
// If no channel is found, none of our connections want to
@@ -113,12 +123,12 @@ protected function onMessage(string $redisChannel, string $payload)
113
123
114
124
$ socket = $ payload ->socket ?? null ;
115
125
116
- // Remove fields intended for internal use from the payload
126
+ // Remove fields intended for internal use from the payload.
117
127
unset($ payload ->socket );
118
128
unset($ payload ->serverId );
119
129
unset($ payload ->appId );
120
130
121
- // Push the message out to connected websocket clients
131
+ // Push the message out to connected websocket clients.
122
132
$ channel ->broadcastToEveryoneExcept ($ payload , $ socket , $ appId , false );
123
133
}
124
134
0 commit comments