pubnub

Get the number of subscribers to a channel in Pubnub


Is there a way to get the number of subscribers subscribed to a specific CHANNEL in Pubnub?

If so, please let me know. Thanks.


Solution

  • Presence - Here Now

    Read all the docs about Presence to get a base understanding of what it offers.

    The hereNow API is what seek. If you need code sample for another language, you can navigate to the other SDKs (languages) on the docs site.

    The old school callback way:

    pubnub.hereNow(
      {
        channels: ["chats.room1", "chats.room2"],
        includeState: true
      },
      function (status, response) {
        console.log(status, response);
      }
    );
    

    The async/await way:

    async function getChannelOccupants {
        try {
            const result = await pubnub.hereNow({
                channels: ["chats.room1", "chats.room2"],
            });
        } 
        catch (status) {
            console.log(status);
        }
    }