In Redis, how can I subscribe to the number of subscribers to a given key (I want to know the number of connected users)? I'm aware of PUBSUB NUMSUB
, but as far as I know it is not possible to subscribe to its value, and polling it every second seems like a huge waste of resource, especially if hundreds/thousands of clients are doing it simultaneously.
Generally no, pubsub
is not persistent in the keyspace so you can't sub to it.
You can add an internal logic on the app side to create and update an extra data-structure in the server for managing subs count yourself, which can be a bit of a challenging task since you need to track failover, disconnections etc. so you don't have false data.
You can refactor to streams which give you better control options over groups and consumers, plus give you better stability.