1 | <?php |
||
13 | class RedisClient implements ReplicationInterface |
||
14 | { |
||
15 | /** |
||
16 | * @var LoopInterface |
||
17 | */ |
||
18 | protected $loop; |
||
19 | |||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $serverId; |
||
24 | |||
25 | /** |
||
26 | * @var Client |
||
27 | */ |
||
28 | protected $publishClient; |
||
29 | |||
30 | /** |
||
31 | * @var Client |
||
32 | */ |
||
33 | protected $subscribeClient; |
||
34 | |||
35 | /** |
||
36 | * Mapping of subscribed channels, where the key is the channel name, |
||
37 | * and the value is the amount of connections which are subscribed to |
||
38 | * that channel. Used to keep track of whether we still need to stay |
||
39 | * subscribed to those channels with Redis. |
||
40 | * |
||
41 | * @var int[] |
||
42 | */ |
||
43 | protected $subscribedChannels = []; |
||
44 | |||
45 | /** |
||
46 | * RedisClient constructor. |
||
47 | */ |
||
48 | public function __construct() |
||
52 | |||
53 | /** |
||
54 | * Boot the RedisClient, initializing the connections. |
||
55 | * |
||
56 | * @param LoopInterface $loop |
||
57 | * @return ReplicationInterface |
||
58 | */ |
||
59 | public function boot(LoopInterface $loop): ReplicationInterface |
||
75 | |||
76 | /** |
||
77 | * Handle a message received from Redis on a specific channel. |
||
78 | * |
||
79 | * @param string $redisChannel |
||
80 | * @param string $payload |
||
81 | */ |
||
82 | protected function onMessage(string $redisChannel, string $payload) |
||
121 | |||
122 | /** |
||
123 | * Subscribe to a channel on behalf of websocket user. |
||
124 | * |
||
125 | * @param string $appId |
||
126 | * @param string $channel |
||
127 | * @return bool |
||
128 | */ |
||
129 | public function subscribe(string $appId, string $channel): bool |
||
142 | |||
143 | /** |
||
144 | * Unsubscribe from a channel on behalf of a websocket user. |
||
145 | * |
||
146 | * @param string $appId |
||
147 | * @param string $channel |
||
148 | * @return bool |
||
149 | */ |
||
150 | public function unsubscribe(string $appId, string $channel): bool |
||
167 | |||
168 | /** |
||
169 | * Publish a message to a channel on behalf of a websocket user. |
||
170 | * |
||
171 | * @param string $appId |
||
172 | * @param string $channel |
||
173 | * @param stdClass $payload |
||
174 | * @return bool |
||
175 | */ |
||
176 | public function publish(string $appId, string $channel, stdClass $payload): bool |
||
185 | |||
186 | /** |
||
187 | * Build the Redis connection URL from Laravel database config. |
||
188 | * |
||
189 | * @return string |
||
190 | */ |
||
191 | protected function getConnectionUri() |
||
209 | } |
||
210 |