1 | <?php |
||
14 | class RedisClient implements ReplicationInterface |
||
15 | { |
||
16 | /** |
||
17 | * @var LoopInterface |
||
18 | */ |
||
19 | protected $loop; |
||
20 | |||
21 | /** |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $serverId; |
||
25 | |||
26 | /** |
||
27 | * @var Client |
||
28 | */ |
||
29 | protected $publishClient; |
||
30 | |||
31 | /** |
||
32 | * @var Client |
||
33 | */ |
||
34 | protected $subscribeClient; |
||
35 | |||
36 | /** |
||
37 | * Mapping of subscribed channels, where the key is the channel name, |
||
38 | * and the value is the amount of connections which are subscribed to |
||
39 | * that channel. Used to keep track of whether we still need to stay |
||
40 | * subscribed to those channels with Redis. |
||
41 | * |
||
42 | * @var int[] |
||
43 | */ |
||
44 | protected $subscribedChannels = []; |
||
45 | |||
46 | /** |
||
47 | * RedisClient constructor. |
||
48 | */ |
||
49 | public function __construct() |
||
53 | |||
54 | /** |
||
55 | * Boot the RedisClient, initializing the connections. |
||
56 | * |
||
57 | * @param LoopInterface $loop |
||
58 | * @return ReplicationInterface |
||
59 | */ |
||
60 | public function boot(LoopInterface $loop): ReplicationInterface |
||
76 | |||
77 | /** |
||
78 | * Handle a message received from Redis on a specific channel. |
||
79 | * |
||
80 | * @param string $redisChannel |
||
81 | * @param string $payload |
||
82 | */ |
||
83 | protected function onMessage(string $redisChannel, string $payload) |
||
122 | |||
123 | /** |
||
124 | * Subscribe to a channel on behalf of websocket user. |
||
125 | * |
||
126 | * @param string $appId |
||
127 | * @param string $channel |
||
128 | * @return bool |
||
129 | */ |
||
130 | public function subscribe(string $appId, string $channel): bool |
||
143 | |||
144 | /** |
||
145 | * Unsubscribe from a channel on behalf of a websocket user. |
||
146 | * |
||
147 | * @param string $appId |
||
148 | * @param string $channel |
||
149 | * @return bool |
||
150 | */ |
||
151 | public function unsubscribe(string $appId, string $channel): bool |
||
168 | |||
169 | /** |
||
170 | * Publish a message to a channel on behalf of a websocket user. |
||
171 | * |
||
172 | * @param string $appId |
||
173 | * @param string $channel |
||
174 | * @param stdClass $payload |
||
175 | * @return bool |
||
176 | */ |
||
177 | public function publish(string $appId, string $channel, stdClass $payload): bool |
||
186 | |||
187 | /** |
||
188 | * Add a member to a channel. To be called when they have |
||
189 | * subscribed to the channel. |
||
190 | * |
||
191 | * @param string $appId |
||
192 | * @param string $channel |
||
193 | * @param string $socketId |
||
194 | * @param string $data |
||
195 | */ |
||
196 | public function joinChannel(string $appId, string $channel, string $socketId, string $data) |
||
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 | */ |
||
209 | public function leaveChannel(string $appId, string $channel, string $socketId) |
||
213 | |||
214 | /** |
||
215 | * Retrieve the full information about the members in a presence channel. |
||
216 | * |
||
217 | * @param string $appId |
||
218 | * @param string $channel |
||
219 | * @return PromiseInterface |
||
220 | */ |
||
221 | public function channelMembers(string $appId, string $channel): PromiseInterface |
||
231 | |||
232 | /** |
||
233 | * Get the amount of users subscribed for each presence channel. |
||
234 | * |
||
235 | * @param string $appId |
||
236 | * @param array $channelNames |
||
237 | * @return PromiseInterface |
||
238 | */ |
||
239 | public function channelMemberCounts(string $appId, array $channelNames): PromiseInterface |
||
252 | |||
253 | /** |
||
254 | * Build the Redis connection URL from Laravel database config. |
||
255 | * |
||
256 | * @return string |
||
257 | */ |
||
258 | protected function getConnectionUri() |
||
276 | } |
||
277 |