Redis publish/subscribe: see what channels are currently subscribed to

后端 未结 5 1658
Happy的楠姐
Happy的楠姐 2021-02-05 14:37

I am currently interested in seeing what channels are subscribed to in a Redis pub/sub application I have. When a client connects to our server, we register them to a channel th

5条回答
  •  滥情空心
    2021-02-05 15:00

    There is currently no command for showing what channels "exist" by way of being subscribed to, but there is and "approved" issue and a pull request that implements this.

    https://github.com/antirez/redis/issues/221
    https://github.com/antirez/redis/pull/412

    Due to the nature of this call, it is not something that can scale, and is thus a "DEBUG" command.

    There are a few other ways to solve your problem, however. If you have reason to believe that a channel may be subscribed to, you can send it a message and look at the result. The result is the number of subscribers that got the message. If you got 0, you know that they're not there.

    Assuming that your user_ids are incremental, you might be interested in using SETBIT to set a 1 or 0 to a user's offset bit to track presence. You can then do cool things like the new BITCOUNT to see how many users are online, and GETBIT to determine if a specific user is online.

    The way I have solved your problem more specifically in the past is by signaling a subscription manager that I have subscribed to a channel. The manager then "pings" the channel by sending a blank message to confirm that there is a subscriber, and occasionally pings the channel thereafter to determine if the user is still online. Not ideal, but better than using DEBUG CHANNELS in production.

提交回复
热议问题