React V4React V4Node.jsPhoneGapWebJavaScriptReact V4 Presence API Reference for Realtime Apps

Go to Publish & Subscribe


Requires Presence add-on XRequires that the Presence add-on is enabled for your key. See this page on enabling add-on features on your keys:

http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.
You can obtain information about the current state of a channel including a list of unique user-ids currently subscribed to the channel and the total occupancy count of the channel by calling the hereNow() function in your application.
To call Here Now you can use the following method(s) in the React V4 SDK:
  1. ParameterTypeRequiredDefaultsDescription
    Operation ArgumentsHashYesA hash of arguments.
    channels ArrayOptionalSpecifies the channel name to return occupancy results. If channel is not provided, hereNow() will return data for all channels.
    channelGroups ArrayOptionalThe channel group for which here now information should be received.
    includeUUIDs Boolean OptionaltrueSetting uuid to false disables the return of uuids.
    includeState BooleanOptionalfalseSetting state to true enables the return of subscriber state information.
    callbackFunctionOptionalExecutes on a successful/unsuccessful hereNow.
this.pubnub.hereNow(
    {
        channels: ["my_channel"],
        channelGroups : ["my_channelGroup"],
        includeUUIDs: true,
        includeState: true
    },
    (status, response) => {
        console.log(status);
        console.log(response);
    }
);
type hereNowResponse = {
    totalChannels: number, // totalChannels = get total of channels
    totalOccupancy: number, // totalOccupancy = get total of occupancies
    channels: object // channels = get a map with values for each channel with uuids and states for each occupant of the channel
}
  1. Requires Presence add-on XRequires that the Presence add-on is enabled for your key. See this page on enabling add-on features on your keys:

    http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.
    pubnub.hereNow(
        {
            channels: ["my_channel"],
            includeState: true
        },
        function (status, response) {
            // handle status, response
        }
    );
    // Example of Status
    {
        "error": false,
        "operation": "PNHereNowOperation",
        "statusCode": 200
    }
    
    // Example of Response
    {
        "totalChannels": 1,
        "totalOccupancy": 3,
        "channels": {
            "my_channel": {
                "occupants": [
                    {
                        "uuid": "User 1"
                    },
                    {
                        "state": {
                            "age": 18
                        },
                        "uuid": "User 2"
                    },
                    {
                        "state": {
                            "age": 24
                        },
                        "uuid": "User 3"
                    }
                ],
                "name": "my_channel",
                "occupancy": 3
            }
        }
    }
  2. Requires Presence add-on XRequires that the Presence add-on is enabled for your key. See this page on enabling add-on features on your keys:

    http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.
    You can return only the occupancy information for a single channel by specifying the channel and setting UUIDs to false:
    pubnub.hereNow(
        {
            channels: ["my_channel"],
            includeUUIDs: false
        },
        function (status, response) {
            // handle status, response
        }
    );
    // Example of Status
    {
        "error": false,
        "operation": "PNHereNowOperation",
        "statusCode": 200
    }
    
    // Example of Response
    {
        "totalChannels": 1,
        "totalOccupancy": 3,
        "channels": {
            "my_channel": {
                "occupants": [],
                "name": "my_channel",
                "occupancy": 3
            }
        }
    }
  3. Requires Presence add-on XRequires that the Presence add-on is enabled with Global Here Now checked for your key. See this page on enabling add-on features on your keys:

    http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.
    You can return the list of UUIDs and occupancy for all channels by omitting the channel:
    pubnub.hereNow(
        {
            includeUUIDs: true
        },
        function (status, response) {
            // handle status, response
        }
    );
    // Example of Status
    {
        "error": false,
        "operation": "PNHereNowOperation",
        "statusCode": 200
    }
    
    // Example of Response
    {
        "totalChannels": 2,
        "totalOccupancy": 3,
        "channels": {
            "my_channel_2": {
                "occupants": [
                    {
                        "state": null,
                        "uuid": "User2"
                    }
                ],
                "name": "my_channel_2",
                "occupancy": 1
            },
            "my_channel_1": {
                "occupants": [
                    {
                        "state": null,
                        "uuid": "User1"
                    },
                    {
                        "state": null,
                        "uuid": "User3"
                    }
                ],
                "name": "my_channel_1",
                "occupancy": 2
            }
        }
    }
  4. Requires Presence add-on XRequires that the Presence add-on is enabled with Global Here Now checked for your key. See this page on enabling add-on features on your keys:

    http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.
    You can return only the occupancy information (Global Here Now) by omitting the channel name
    pubnub.hereNow(
        {
            includeUUIDs: true,
            includeState: true
        },
        function (status, response) {
            // handle status, response
        }
    );
    // Example of Status
    {
        "error": false,
        "operation": "PNHereNowOperation",
        "statusCode": 200
    }
    
    // Example of Response
    {
        "totalChannels": 2,
        "totalOccupancy": 3,
        "channels": {
            "my_channel_2": {
                "occupants": [
                    {
                        "uuid": "User2"
                    }
                ],
                "name": "my_channel_2",
                "occupancy": 1
            },
            "my_channel_1": {
                "occupants": [
                    {
                        "state": {
                            "age": 24
                        },
                        "uuid": "User 3"
                    },
                    {
                        "uuid": "User1"
                    }
                ],
                "name": "my_channel_1",
                "occupancy": 2
            }
        }
    }
  5. Requires Presence add-on XRequires that the Presence add-on is enabled for your key. See this page on enabling add-on features on your keys:

    http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.
    pubnub.hereNow(
        {
            channelGroups : ["my_channel_group"]
        },
        function (status, response) {
            // handle status, response
        }
    );
    // Example of Status
    {
        "error": false,
        "operation": "PNHereNowOperation",
        "statusCode": 200
    }
    
    // Example of Response
    {
        "totalChannels": 2,
        "totalOccupancy": 3,
        "channels": {
            "my_channel_1": {
                "occupants": [
                    {
                        "state": null,
                        "uuid": "User1"
                    },
                    {
                        "state": null,
                        "uuid": "User3"
                    }
                ],
                "name": "my_channel_1",
                "occupancy": 2
            },
            "my_channel_2": {
                "occupants": [
                    {
                        "state": null,
                        "uuid": "User2"
                    }
                ],
                "name": "my_channel_2",
                "occupancy": 1
            }
        }
    }
  6. Requires Presence add-on XRequires that the Presence add-on is enabled for your key. See this page on enabling add-on features on your keys:

    http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.
    pubnub.hereNow({
        channels: ["ch1"], 
        channelGroups : ["cg1"],
        includeUUIDs: true,
        includeState: true
    }).then((response) => { 
        console.log(response) 
    }).catch((error) => { 
        console.log(error)
    });
  7. Requires Presence add-on XRequires that the Presence add-on is enabled for your key. See this page on enabling add-on features on your keys:

    http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.
    pubnub.hereNow({
        channels: ["my_channel"],
        includeState: true
    }).then((response) => { 
        console.log(response) 
    }).catch((error) => { 
        console.log(error) 
    });
  8. Requires Presence add-on XRequires that the Presence add-on is enabled for your key. See this page on enabling add-on features on your keys:

    http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.
    You can return only the occupancy information for a single channel by specifying the channel and setting uuids to false:
    pubnub.hereNow({
        channels: ["my_channel"],
        includeUUIDs: false
    }).then((response) => { 
        console.log(response) 
    }).catch((error) => { 
        console.log(error) 
    });
  9. Requires Presence add-on XRequires that the Presence add-on is enabled with Global Here Now checked for your key. See this page on enabling add-on features on your keys:

    http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.
    You can return the list of uuids and occupancy for all channels by omitting the channel:
    pubnub.hereNow({
        includeUUIDs: true
    }).then((response) => { 
        console.log(response) 
    }).catch((error) => { 
        console.log(error) 
    });
  10. Requires Presence add-on XRequires that the Presence add-on is enabled with Global Here Now checked for your key. See this page on enabling add-on features on your keys:

    http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.
    You can return only the occupancy information (Global Here Now) by omitting the channel name
    pubnub.hereNow({
        includeUUIDs: true,
        includeState: true
    }).then((response) => { 
        console.log(response) 
    }).catch((error) => { 
        console.log(error) 
    });
  11. Requires Presence add-on XRequires that the Presence add-on is enabled for your key. See this page on enabling add-on features on your keys:

    http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.
    pubnub.hereNow({
        channelGroups : ["my_channel_group"]
    }).then((response) => { 
        console.log(response) 
    }).catch((error) => { 
        console.log(error) 
    });
Requires Presence add-on XRequires that the Presence add-on is enabled for your key. See this page on enabling add-on features on your keys:

http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.
You can obtain information about the current list of channels to which a UUID is subscribed to by calling the whereNow function in your application.
 
If the app is killed/crashes and restarted (or the page containing the PubNub instance is refreshed on the browser) within the heartbeat window no timeout event is generated.
To call whereNow you can use the following method(s) in the React V4 SDK:
  1. ParameterTypeRequiredDefaultsDescription
    Operation ArgumentsHashYesA hash of arguments.
    uuid String Optionalcurrent uuidSpecifies the uuid to return channel list for.
    callbackFunctionOptionalExecutes on a successful/unsuccessful whereNow.
You simply need to define the uuid and the callback function to be used to send the data to as in the example below.
pubnub.whereNow(
    {
        uuid: "uuid"
    },
    function (status, response) {
        // handle status, response
    }
);
// Example of Status
{
    error: false,
    operation: "PNWhereNowOperation",
    statusCode: 200
}

// Example of Response
{
    channels: ["ch1"]
}
  1. pubnub.whereNow({ 
        uuid: "uuid"
    }).then((response) => {
        console.log(response);
    }).catch((error) => {
        console.log(error);
    });
Requires Presence add-on XRequires that the Presence add-on is enabled for your key. See this page on enabling add-on features on your keys:

http://www.pubnub.com/knowledge-base/discussion/644/how-do-i-enable-add-on-features-for-my-keys.
The state API is used to set/get key/value pairs specific to a subscriber uuid.
State information is supplied as a JSON object of key/value pairs.
  1. ParameterTypeRequiredDescription
    Operation ArgumentsHashYesA hash of arguments.
    channelsArrayOptionalEither channels or channelGroups should be provided, Specifies the channels to set the state.
    channelGroupsArrayOptionalEither channels or channelGroups should be provided, Specifies the Channel Group to set the state
    state ObjectOptional

    JSON object of key/value pairs with supported data-types of int, float and string. Nesting of key/values is not permitted and key names beginning with prefix pn are reserved.

    If the state parameter is undefined, the current state for the specified uuid will be returned. If a specified key already exists for the uuid it will be over-written with the new value. Key values can be deleted by setting the particular value to null.

    callbackFunctionOptionalExecutes on a successful/unsuccessful setState.
  1. ParameterTypeRequiredDefaultsDescription
    Operation ArgumentsHashYesA hash of arguments.
    uuidStringOptionalcurrent uuidThe subscriber uuid to get the current state.
    channelsArrayOptionalEither channels or channelGroups should be provided, Specifies the channels to get the state.
    channelGroups ArrayOptionalEither channels or channelGroups should be provided, Specifies the Channel Group to get the state.
    callbackFunctionOptionalExecutes on a successful/unsuccessful getState.
pubnub.setState(
    {
        state: newState,
        channels: ['ch1'],
        channelGroups: ['cg1']
    },
    function (status, response) {
        // handle status, response
    }
);
pubnub.getState(
    {
        uuid: "uuid",
        channels: ['ch1'],
        channelGroups: ['cg1']
    },
    function (status, response) {
        // handle status, response
    }
);
// Example of Status
{
    error: false,
    operation: "PNSetStateOperation",
    statusCode: 200
}

// Example of Response
{
    state: {
        me: 'typing'
    } 
}
// Example of Status
{
    error: false,
    operation: "PNGetStateOperation",
    statusCode: 200
}

// Example of Response
{
    channels: {
        ch1: {
            me: 'typing'
        }
    }
}
  1. pubnub.setState({
        state: newState,
        channels: ['ch1'],
        channelGroups: ['cg1']
    }).then((response) => { 
        console.log(response) 
    }).catch((error) => { 
        console.log(error)
    });
  2. pubnub.getState({
        uuid: "uuid",
        channels: ['ch1'],
        channelGroups: ['cg1']
    }).then((response) => { 
        console.log(response) 
    }).catch((error) => { 
        console.log(error)
    });

Go to Access Manager