Presence API for AngularJS SDK
Presence enables you to track the online and offline status of users and devices in real time, as well as store custom state information. Presence provides authoritative information on:
Presence lets you track who is online or offline and store custom state information. Presence shows:
- When a user has joined or left a channel
- Who, and how many, users are subscribed to a particular channel
- Which channel(s) an individual user is subscribed to
- Associated state information for these users
Learn more about our Presence feature in the Presence overview.
Here now
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
This method returns information about the current state of a channel, including a list of unique user IDs (universally unique identifiers, UUIDs) currently subscribed to the channel and the total occupancy count of the channel.
Cache
This method has a 3-second response cache time.
Method(s)
Use the following method in the AngularJS SDK:
hereNow( {Array channels, Array channelGroups, Boolean includeUUIDs, Boolean includeState}, Function callback )
Parameter | Description |
---|---|
channels Type: Array Default: n/a | Specifies the channel name to return occupancy results. If channel is not provided, hereNow() will return data for all channels . |
channelGroups Type: Array Default: n/a | The channel group for which here now information should be received. |
includeUUIDs Type: Boolean Default: true | Setting uuid to false disables the return of uuids. |
includeState Type: Boolean Default: false | Setting state to true enables the return of subscriber state information. |
callback Type: Function Default: n/a | Executes on a successful/unsuccessful hereNow . |
Sample code
Pubnub.hereNow(
{
channels: ["ch1"],
channelGroups : ["cg1"],
includeUUIDs: true,
includeState: true
},
function (status, response) {
// handle status, response
}
);
Response
type HereNowResponse = {
channels: Array<string>,
}
Other examples
Returning state
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
pubnub.hereNow(
{
channels: ["my_channel"],
includeState: true
},
function (status, response) {
// handle status, response
}
);
Example Response:
// Example of Status
{
"error": false,
"operation": "PNHereNowOperation",
"statusCode": 200
}
// Example of Response
{
"totalChannels": 1,
"totalOccupancy": 3,
"channels": {
"my_channel": {
"occupants": [
{
show all 35 linesReturn occupancy only
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
To return only occupancy
for a channel, set includeUUIDs
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",
show all 19 linesChannel group usage
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
Pubnub.hereNow(
{
channelGroups : ["my_channel_group"]
},
function (status, response) {
// handle status, response
}
);
Example Response:
// Example of Status
{
"error": false,
"operation": "PNHereNowOperation",
"statusCode": 200
}
// Example of Response
{
"totalChannels": 2,
"totalOccupancy": 3,
"channels": {
"my_channel_1": {
"occupants": [
{
show all 38 linesWhere now
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
This method returns the list of channels a UUID is subscribed to.
Note
If the app restarts (or the page refreshes) within the heartbeat window, no timeout event is generated.
Method(s)
Use the following method in the AngularJS SDK:
whereNow ({String uuid},Function callback)
Parameter | Description |
---|---|
uuid Type: String Default: current uuid | Specifies the uuid to return channel list for. |
callback Type: Function Default: n/a | Executes on a successful/unsuccessful whereNow . |
Sample code
You simply need to define the uuid
and the callback
function to be used to send the data to as in the example below.
// Get List of channels for uuid.
Pubnub.whereNow(
{
uuid: 'my_uuid'
},
function (status, response) {
// handle status, response
}
);
Response
// Example of Status
{
error: false,
operation: "PNWhereNowOperation",
statusCode: 200
}
// Example of Response
{
"channels": ["ch1", "ch2"]
}
User state
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
Description
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.
Method(s)
Set state
setState({Array channels, Array channelGroups, Object state},Function callback)
Parameter | Description |
---|---|
channels Type: Array | Either channels or channelGroups should be provided, Specifies the channels to set the state. |
channelGroups Type: Array | Either channels or channelGroups should be provided, Specifies the Channel Group to set the state |
state Type: Object | 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 . |
callback Type: Function | Executes on a successful/unsuccessful setState . |
Get state
getState({String uuid, Arraychannels, ArraychannelGroups},Function callback)
Parameter | Description |
---|---|
uuid Type: String Default: current uuid | The subscriber uuid to get the current state. |
channels Type: Array Default: n/a | Either channels or channelGroups should be provided, Specifies the channels to get the state. |
channelGroups Type: Array Default: n/a | Either channels or channelGroups should be provided, Specifies the Channel Group to get the state. |
callback Type: Function Default: n/a | Executes on a successful/unsuccessful getState . |
Sample code
Set state
Pubnub.setState(
{
state: {
"Key": "Value"
},
channels: ["my_channel"]
},
function (status, response) {
// handle status, response
}
);
Get state
Pubnub.getState(
{
uuid : "my_uuid",
channels : ["my_channel"]
},
function (status, response) {
// handle status, response
}
);
Response
Set state
// Example of Status
{
error: false,
operation: "PNSetStateOperation",
statusCode: 200
}
// Example of Response
{
state: {
me: 'typing'
}
}
Get state
// Example of Status
{
error: false,
operation: "PNGetStateOperation",
statusCode: 200
}
// Example of Response
{
channels: {
ch1: {
me: 'typing'
}
}
}