Presence

Presence enables you to track the online and offline status of users and devices in real time and store custom state information. When you have Presence enabled, PubNub automatically creates a presence channel for each channel. Subscribing to a presence channel or presence channel group will only return presence events

Requires Presence add-on

This method requires that the Presence add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

State Shape

The data about Presence is stored as a list of spaces and the list of occupants (members) in each space. The following example shows the shape of Presence data in the store:

{
"byId": {
"space_ac4e67b98b34b44c4a39466e93e": {
"name": "space_ac4e67b98b34b44c4a39466e93e",
"occupants": [
{
"uuid": "user_53bbe00387004010a8b9ad5f36bdd4a7"
}
],
"occupancy": 1
}
}
}

Reducers

The PubNub Redux framework provides reducers your app can implement that respond to various actions that update the store. To track the state of a set of objects in the store, combine the reducers you want into the rootReducer for your app.

createPresenceReducer

createPresenceReducer instantiates a reducer in the store that responds to actions dispatched to update the state of a user's presence in the store.

createPresenceReducer();

Listeners

The PubNub Redux framework includes listeners that monitor PubNub events from the server and dispatch corresponding actions. All listeners are automatically invoked if your app registers the combined PubNub listener. You can register only specific listeners, or implement your own combine listeners function.

createPresenceListener

createPresenceListener registers a listener in the store that monitors Feature events.

A sample implementation of a single listener:

pubnub.addListener(createPresenceListener(store.dispatch));

Commands

The PubNub Redux framework provides commands that your app can dispatch to the store to be managed by the Thunk middleware. Commands interact with the PubNub API and dispatch basic actions which are then processed by reducers to update the state in the store.

fetchHereNow

Obtain information about the current state of a channel, including a list of unique userIds currently subscribed to the channel and the total occupancy count of the channel.

fetchHereNow( request, [meta] );

fetchHereNow Arguments

ParameterTypeRequiredDescription
requestHereNowRequestYesSpace request parameter object.
[meta]objectOptionalStandard meta options object.

fetchHereNow HereNowRequest Properties

PropertyTypeRequiredDefaultDescription
[channels]ArrayOptionalSpecifies the channel name to return occupancy results. If channel isn't specified, request the data for all channels.
[channelGroups]ArrayOptionalThe channel group for which here now information should be received.
[includeUUIDs]booleanOptionaltrueSetting false to disable the return of UUIDs.
[includeState]booleanOptionalfalseSet to true to enable the return of subscriber state information.

fetchHereNow Sample Usage

dispatch(fetchHereNow({
channels: [ "space_ac4e67b98b34b44c4a39466e93e" ]
}));

fetchPresenceState

Get the current state information based on the current uuid for either specified channels or channelGroups.

fetchPresenceState( request, [meta] );

fetchPresenceState Arguments

ParameterTypeRequiredDescription
requestPresenceStateRequestYesSpace request parameter object.
[meta]objectOptionalStandard meta options object.

fetchPresenceState PresenceStateRequest Properties

PropertyTypeRequiredDefaultDescription
uuidstringOptionalcurrent UUIDThe subscriber UUID to get the current state.
channelsArrayOptionalSpecifies the channels to get the state. Either channels or channelGroups should be specified.
channelGroupsArrayOptionalSpecifies the channels to get the state. Either channels or channelGroups should be specified.

fetchPresenceState Sample Usage

dispatch(fetchPresenceState({
uuid: "user_53bbe00387004010a8b9ad5f36bdd4a7",
channels: ['space_ac4e67b98b34b44c4a39466e93e'],
}));
Last updated on