usePresence for PubNub Chat Components for React Native

Migrate to Chat SDK

PubNub will stop supporting Chat Components on January 1, 2025 but you are welcome to contribute. Learn how to migrate to the Chat SDK here.

The hook returns the list of unique user IDs currently subscribed to the channel.

This hook also sets up a listener that reacts to new presence events (joins and leaves/timeouts). However, this behavior requires a living subscription to the channels passed to the hook - this should be handled by the components.

Information returned by this hook can be used to show a presence indicator, or a number of people currently subscribed to the channel.

const [channels, refetchPresence, totalPresence, error, isLoading] = usePresence({
channels: ["test-channel"],
});

return <p>Occupancy of the channel: {channels["test-channel"].occupancy}</p>;

Input

User ID / UUID

User ID is also referred to as UUID/uuid in some APIs and server responses but holds the value of the userId parameter you set during initialization.

ParameterTypeRequiredDefaultsDescription
channelsArrayOptionaln/aChannel name to return occupancy results. If channel is not provided, hereNow() will return data for all channels.
channelGroupsArrayOptionaln/aChannel group for which hereNow() information should be received.
includeUUIDsBooleanOptionaltrueOption to disable returning User IDs.
includeStateBooleanOptionalfalseOption to enable returning subscriber state information.

Output

ParameterTypeDescription
array[0]ChannelPresence[]List of returned channel presence data.
array[1]FunctionRefetching presence from scratch. This is rarely needed since the hook sets up presence listeners to get updates.
array[2]NumberTotal number of present users across all passed channels.
array[3]ErrorIf there's an error fetching presence, it will be available here.
array[4]BooleanIndicator that the channel presence data is still being loaded.

ChannelPresence interface

  [channel: string]: {
name: string;
occupancy: number;
occupants?: {
uuid: string;
state?: unknown;
}[];
};
Last updated on