usePresence for PubNub Chat Components for React Native

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] = usePresence({
channels: ["test-channel"],
});

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

Input

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 uuids.
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.

ChannelPresence interface

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