Get online users in a channel

Showing JavaScript examples.

This guide shows you how to call Here Now to find out who is currently subscribed to a channel. A response gives you the total occupancy, each subscriber's User ID, and, if set, their presence state. Presence must be enabled on the keyset, and new keysets have it enabled by default.

  • Fetch the occupant list and state for one or more channels.
  • Fetch only the occupancy count, without the member list.
  • Query a channel group instead of a fixed list of channels.
  • Page past the per-call occupant limit.

If Presence Management restricts tracking to selected channels only, the channel you query with Here Now must be one of them. When Access Manager is enabled, calling Here Now requires read permission on the queried channel.

Get the occupant list and state​

Call Here Now and set includeState (named include_state in some SDKs) to fetch each occupant's User ID and presence state alongside the channel's occupancy.

1

Most other SDKs document the same call in the Here Now section of their Presence API reference, for example PHP or Ruby.

An occupant with no state set returns null (or your language's equivalent empty value) instead of an object. Requesting state costs nothing extra when the channel has none to return.

Get only the occupancy count​

Set includeUUIDs (or include_uuids) to false, or limit to 0, when you only need the number, such as an "N people online" badge that never lists who they are.

1

The response shape stays the same size regardless of how many clients are on the channel, because no member list comes back. Refer to Occupancy for why that matters on a busy channel, and to your platform's API reference for the exact parameter name.

Query a channel group instead of listing channels​

Pass a channel group name instead of a channel list when your subscribers use a wildcard pattern. Here Now doesn't accept a wildcard itself, only a fixed channel, a list of channels, or a channel group.

1

Refer to Channels and channel naming for how one naming convention feeds both a wildcard subscription and the channel group you query here.

Page past the occupant limit​

A single Here Now call returns up to 1,000 occupants per channel, and you paginate past that with offset. Fetch successive pages by increasing offset by your limit each time, until a response returns fewer occupants than you asked for.

1// First page: occupants 0-99
2const page1 = await pubnub.hereNow({ channels: ['chats.megachat'], limit: 100 });
3
4// Next page: occupants 100-199
5const page2 = await pubnub.hereNow({ channels: ['chats.megachat'], limit: 100, offset: 100 });

The occupancy field on every page reports the same true, uncapped total. Only the occupants array is paginated.

Debug Console

To check who is subscribed to a channel without writing code, use the Presence tab of the Debug Console in the Admin Portal.

Was this page useful?

Last updated on