@@ -175,6 +175,36 @@ public function unsubscribe($appId, string $channel): bool
175
175
return true ;
176
176
}
177
177
178
+ /**
179
+ * Subscribe to the app's pubsub keyspace.
180
+ *
181
+ * @param mixed $appId
182
+ * @return bool
183
+ */
184
+ public function subscribeToApp ($ appId ): bool
185
+ {
186
+ $ this ->subscribeClient ->__call ('subscribe ' , [$ this ->getTopicName ($ appId )]);
187
+
188
+ $ this ->publishClient ->__call ('hincrby ' , [$ this ->getTopicName ($ appId ), 'connections ' , 1 ]);
189
+
190
+ return true ;
191
+ }
192
+
193
+ /**
194
+ * Unsubscribe from the app's pubsub keyspace.
195
+ *
196
+ * @param mixed $appId
197
+ * @return bool
198
+ */
199
+ public function unsubscribeFromApp ($ appId ): bool
200
+ {
201
+ $ this ->subscribeClient ->__call ('unsubscribe ' , [$ this ->getTopicName ($ appId )]);
202
+
203
+ $ this ->publishClient ->__call ('hincrby ' , [$ this ->getTopicName ($ appId ), 'connections ' , -1 ]);
204
+
205
+ return true ;
206
+ }
207
+
178
208
/**
179
209
* Add a member to a channel. To be called when they have
180
210
* subscribed to the channel.
@@ -258,6 +288,17 @@ public function channelMemberCounts($appId, array $channelNames): PromiseInterfa
258
288
});
259
289
}
260
290
291
+ /**
292
+ * Get the amount of connections aggregated on multiple instances.
293
+ *
294
+ * @param mixed $appId
295
+ * @return null|int|\React\Promise\PromiseInterface
296
+ */
297
+ public function getGlobalConnectionsCount ($ appId )
298
+ {
299
+ return $ this ->publishClient ->hget ($ this ->getTopicName ($ appId ), 'connections ' );
300
+ }
301
+
261
302
/**
262
303
* Handle a message received from Redis on a specific channel.
263
304
*
@@ -321,8 +362,8 @@ public function onMessage(string $redisChannel, string $payload)
321
362
*/
322
363
protected function getConnectionUri ()
323
364
{
324
- $ name = config ('websockets.replication.redis.connection ' ) ?: 'default ' ;
325
- $ config = config (' database.redis ' )[ $ name] ;
365
+ $ name = config ('websockets.replication.redis.connection ' , 'default ' ) ;
366
+ $ config = config (" database.redis. { $ name}" ) ;
326
367
327
368
$ host = $ config ['host ' ];
328
369
$ port = $ config ['port ' ] ?: 6379 ;
@@ -377,13 +418,19 @@ public function getServerId()
377
418
* app ID and channel name.
378
419
*
379
420
* @param mixed $appId
380
- * @param string $channel
421
+ * @param string|null $channel
381
422
* @return string
382
423
*/
383
- protected function getTopicName ($ appId , string $ channel ): string
424
+ protected function getTopicName ($ appId , string $ channel = null ): string
384
425
{
385
426
$ prefix = config ('database.redis.options.prefix ' , null );
386
427
387
- return "{$ prefix }{$ appId }: {$ channel }" ;
428
+ $ hash = "{$ prefix }{$ appId }" ;
429
+
430
+ if ($ channel ) {
431
+ $ hash .= ": {$ channel }" ;
432
+ }
433
+
434
+ return $ hash ;
388
435
}
389
436
}
0 commit comments