1 | <?php |
||
15 | class RedisClient extends LocalClient |
||
16 | { |
||
17 | /** |
||
18 | * The running loop. |
||
19 | * |
||
20 | * @var LoopInterface |
||
21 | */ |
||
22 | protected $loop; |
||
23 | |||
24 | /** |
||
25 | * The unique server identifier. |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $serverId; |
||
30 | |||
31 | /** |
||
32 | * The pub client. |
||
33 | * |
||
34 | * @var Client |
||
35 | */ |
||
36 | protected $publishClient; |
||
37 | |||
38 | /** |
||
39 | * The sub client. |
||
40 | * |
||
41 | * @var Client |
||
42 | */ |
||
43 | protected $subscribeClient; |
||
44 | |||
45 | /** |
||
46 | * Mapping of subscribed channels, where the key is the channel name, |
||
47 | * and the value is the amount of connections which are subscribed to |
||
48 | * that channel. Used to keep track of whether we still need to stay |
||
49 | * subscribed to those channels with Redis. |
||
50 | * |
||
51 | * @var int[] |
||
52 | */ |
||
53 | protected $subscribedChannels = []; |
||
54 | |||
55 | /** |
||
56 | * Create a new Redis client. |
||
57 | * |
||
58 | * @return void |
||
|
|||
59 | */ |
||
60 | public function __construct() |
||
64 | |||
65 | /** |
||
66 | * Boot the RedisClient, initializing the connections. |
||
67 | * |
||
68 | * @param LoopInterface $loop |
||
69 | * @param string|null $factoryClass |
||
70 | * @return ReplicationInterface |
||
71 | */ |
||
72 | public function boot(LoopInterface $loop, $factoryClass = null): ReplicationInterface |
||
91 | |||
92 | /** |
||
93 | * Publish a message to a channel on behalf of a websocket user. |
||
94 | * |
||
95 | * @param string $appId |
||
96 | * @param string $channel |
||
97 | * @param stdClass $payload |
||
98 | * @return bool |
||
99 | */ |
||
100 | public function publish($appId, string $channel, stdClass $payload): bool |
||
118 | |||
119 | /** |
||
120 | * Subscribe to a channel on behalf of websocket user. |
||
121 | * |
||
122 | * @param string $appId |
||
123 | * @param string $channel |
||
124 | * @return bool |
||
125 | */ |
||
126 | public function subscribe($appId, string $channel): bool |
||
145 | |||
146 | /** |
||
147 | * Unsubscribe from a channel on behalf of a websocket user. |
||
148 | * |
||
149 | * @param string $appId |
||
150 | * @param string $channel |
||
151 | * @return bool |
||
152 | */ |
||
153 | public function unsubscribe($appId, string $channel): bool |
||
177 | |||
178 | /** |
||
179 | * Add a member to a channel. To be called when they have |
||
180 | * subscribed to the channel. |
||
181 | * |
||
182 | * @param string $appId |
||
183 | * @param string $channel |
||
184 | * @param string $socketId |
||
185 | * @param string $data |
||
186 | * @return void |
||
187 | */ |
||
188 | public function joinChannel($appId, string $channel, string $socketId, string $data) |
||
189 | { |
||
190 | $this->publishClient->__call('hset', ["{$appId}:{$channel}", $socketId, $data]); |
||
191 | |||
192 | DashboardLogger::log($appId, DashboardLogger::TYPE_REPLICATOR_JOINED_CHANNEL, [ |
||
193 | 'channel' => $channel, |
||
194 | 'serverId' => $this->getServerId(), |
||
195 | 'socketId' => $socketId, |
||
196 | 'data' => $data, |
||
197 | 'pubsub' => $this->getTopicName($appId, $channel), |
||
198 | ]); |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * Remove a member from the channel. To be called when they have |
||
203 | * unsubscribed from the channel. |
||
204 | * |
||
205 | * @param string $appId |
||
206 | * @param string $channel |
||
207 | * @param string $socketId |
||
208 | * @return void |
||
209 | */ |
||
210 | public function leaveChannel($appId, string $channel, string $socketId) |
||
211 | { |
||
212 | $this->publishClient->__call('hdel', [$this->getTopicName($appId, $channel), $socketId]); |
||
213 | |||
214 | DashboardLogger::log($appId, DashboardLogger::TYPE_REPLICATOR_LEFT_CHANNEL, [ |
||
215 | 'channel' => $channel, |
||
216 | 'serverId' => $this->getServerId(), |
||
217 | 'socketId' => $socketId, |
||
218 | 'pubsub' => $this->getTopicName($appId, $channel), |
||
219 | ]); |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Retrieve the full information about the members in a presence channel. |
||
224 | * |
||
225 | * @param string $appId |
||
226 | * @param string $channel |
||
227 | * @return PromiseInterface |
||
228 | */ |
||
229 | public function channelMembers($appId, string $channel): PromiseInterface |
||
239 | |||
240 | /** |
||
241 | * Get the amount of users subscribed for each presence channel. |
||
242 | * |
||
243 | * @param string $appId |
||
244 | * @param array $channelNames |
||
245 | * @return PromiseInterface |
||
246 | */ |
||
247 | public function channelMemberCounts($appId, array $channelNames): PromiseInterface |
||
260 | |||
261 | /** |
||
262 | * Handle a message received from Redis on a specific channel. |
||
263 | * |
||
264 | * @param string $redisChannel |
||
265 | * @param string $payload |
||
266 | * @return void |
||
267 | */ |
||
268 | public function onMessage(string $redisChannel, string $payload) |
||
316 | |||
317 | /** |
||
318 | * Build the Redis connection URL from Laravel database config. |
||
319 | * |
||
320 | * @return string |
||
321 | */ |
||
322 | protected function getConnectionUri() |
||
344 | |||
345 | /** |
||
346 | * Get the Subscribe client instance. |
||
347 | * |
||
348 | * @return Client |
||
349 | */ |
||
350 | public function getSubscribeClient() |
||
354 | |||
355 | /** |
||
356 | * Get the Publish client instance. |
||
357 | * |
||
358 | * @return Client |
||
359 | */ |
||
360 | public function getPublishClient() |
||
364 | |||
365 | /** |
||
366 | * Get the unique identifier for the server. |
||
367 | * |
||
368 | * @return string |
||
369 | */ |
||
370 | public function getServerId() |
||
374 | |||
375 | /** |
||
376 | * Get the Pub/Sub Topic name to subscribe based on the |
||
377 | * app ID and channel name. |
||
378 | * |
||
379 | * @param mixed $appId |
||
380 | * @param string $channel |
||
381 | * @return string |
||
382 | */ |
||
383 | protected function getTopicName($appId, string $channel): string |
||
389 | } |
||
390 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.