SelectAngularJS Presence API Reference for Realtime Apps

These docs are for version 3.2.1 of the AngularJS SDK. To check out the docs for the latest version Click Here.
 

PubNub 4.0 for AngularJS is our latest and greatest! Please click here for our PubNub 4.0 for AngularJS docs and SDK.

PubNub 3.x for AngularJS will encounter End Of Life (EOL) July 1st, 2017. All users currently on 3.x should begin migrating to 4.x. The 3.x SDK will continue to work. We will no longer support it and if you run into an issue, we will request that you upgrade to the latest version before solving the issue.

If you have questions about the PubNub for AngularJS SDK, please contact us at support@pubnub.com.

Presence enables you to track the online and offline status of users and devices in realtime, as well as store custom state information. Presence provides authoritative information on:

  • 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 here.


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 here_now() function in your application.
To call Here Now you can use the following method(s) in the AngularJS SDK:
  1. ParameterTypeRequiredDescription
    callback FunctionYesCallback is called on receiving here_now response.
    channel StringYesSpecifies the channel name to return occupancy results. If channel is not provided, here_now will return data for all channels.
    channel_group StringOptionalThe channel group for which here now information should be received.
    state Boolean OptionalSetting state to true enables the return of subscriber state information. Default is false.
    uuids Boolean OptionalSetting uuid to false disables the return of uuids. Default is true.
    error Function OptionalCallback is called on error to here_now request.
// Get List of Occupants and Occupancy Count.
 
Pubnub.here_now({
	channel : 'my_channel',
	callback : function(m){
		console.log(m)
	}
});
The here_now() function returns a list of uuid s currently subscribed to the channel.
  • uuids:["String","String", ... ,"String"] - List of UUIDs currently subscribed to the channel.
  • occupancy: Number - Total current occupancy of the channel.

{
	occupancy : 4,
	uuids : ['123123234t234f34fq3dq', '143r34f34t34fq34q34q3', '23f34d3f4rq34r34rq23q', 'w34tcw45t45tcw435tww3']
}
  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.
    //  Return state
     
    Pubnub.here_now({
    	channel : 'my_channel',
    	state: true,
    	callback : function(m){
    		console.log(m)
    	}
    });
    {
    	"status" : 200,
    	"message" : "OK",
    	"service" : "Presence",
    	"uuids" : [
    		{
    			"uuid" : "myUUID0"
    		},
    		{
    			"state" : {
    				"abcd" : {
    					"age" : 15
    				}
    			},
    			"uuid" : "myUUID1"
    		},
    		{
    			"uuid" : "b9eb408c-bcec-4d34-b4c4-fabec057ad0d"
    		},
    		{
    			"state" : {
    				"abcd" : {
    					"age" : 15
    				}
    			},
    			"uuid" : "myUUID2"
    		},
    		{
    			"state" : {
    				"abcd" : {
    					"age" : 24
    				}
    			},
    			"uuid" : "myUUID9"
    		}
    	],
    	"occupancy" : 5
    }
  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.here_now({
    	channel : 'my_channel',
    	uuids: false,
    	callback : function(m){
    		console.log(m)
    	}
    });
    {
        "status": 200,
        "message": "OK",
        "payload": {
            "channels": {
                "81d8d989-b95f-443c-a726-04fac323b331": { 
                    "uuids": [ "70fc1140-22b5-4abc-85b2-ff8c17b24d59" ], 
                    "occupancy": 1 
                },
                "81b7a746-d153-49e2-ab70-3037a75cec7e": { 
                    "uuids": [ "91363e7f-584b-49cc-822c-52c2c01e5928" ],
                    "occupancy": 1
                },
                "c068d15e-772a-4bcd-aa27-f920b7a4eaa8": { 
                    "uuids": [ "ccfac1dd-b3a5-4afd-9fd9-db11aeb65395" ],
                    "occupancy": 1
                }
            },
            "total_channels": 3,
            "total_occupancy": 3
        },
        "service": "Presence"
    }
  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.here_now({
    	uuids: true,
    	callback : function(m){
    		console.log(m)
    	}
    });
    
    {
        "total_channels" : 2,
        "total_occupancy" : 3,
        "channels" : {
            "lobby" : {
                "occupancy" : 1,
                "uuids" : [
                    "dara01"
                ]
            },
            "game01" : {
                "occupancy" : 2,
                "uuids" : [
                    "jason01",
                    "jason02"
                ]
            }
        }
    }
  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.here_now({
    	callback : function(m){
    		console.log(m)
    	}
    });
    {
        "status": 200,
        "message": "OK",
        "payload": {
            "channels": {
                "81d8d989-b95f-443c-a726-04fac323b331": { 
                    "uuids": [ "70fc1140-22b5-4abc-85b2-ff8c17b24d59" ], 
                    "occupancy": 1 
                },
                "81b7a746-d153-49e2-ab70-3037a75cec7e": { 
                    "uuids": [ "91363e7f-584b-49cc-822c-52c2c01e5928" ],
                    "occupancy": 1
                },
                "c068d15e-772a-4bcd-aa27-f920b7a4eaa8": { 
                    "uuids": [ "ccfac1dd-b3a5-4afd-9fd9-db11aeb65395" ],
                    "occupancy": 1
                }
            },
            "total_channels": 3,
            "total_occupancy": 3
        },
        "service": "Presence"
    }
  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.
    // Get List of Occupants and Occupancy Count.
    
    pubnub.here_now({
        channel_group : 'my_channel_group',
        callback : function(m){
            console.log(m)
        }
    });
    {
    	"status": 200,
    	"message": "OK",
    	"payload": {
    		"total_occupancy": 2,
    		"total_channels": 2,
    		"channels": {
    			"ch1": {
    				"occupancy": 1,
    				"uuids": [
    					{
    						"uuid": "user1",
    						"state": {
    							"age": 10
    						}
    					}
    				]
    			},
    			"ch2": {
    				"occupancy": 1,
    				"uuids": [
    					{
    						"uuid": "user1",
    						"state": {
    							"age": 10
    						}
    					}
    				]
    			}
    		}
    	},
    	"service": "Presence"
    }
Return Occupancy for all channels by calling the here_now function in your application.
To call Global Here Now you can use the following method(s) in the AngularJS SDK:
  1. You can call Global Here Now by using the same method as Here Now
Pubnub.here_now({
	callback : function(m){
		console.log(m)
	}
});

{
	"status": 200,
	"message": "OK",
	"payload": {
		"channels": {
			"81d8d989-b95f-443c-a726-04fac323b331": {
				"uuids": [
					"70fc1140-22b5-4abc-85b2-ff8c17b24d59"
				],
				"occupancy": 1
			},
			"81b7a746-d153-49e2-ab70-3037a75cec7e": {
				"uuids": [
					"91363e7f-584b-49cc-822c-52c2c01e5928"
				],
				"occupancy": 1
			},
			"c068d15e-772a-4bcd-aa27-f920b7a4eaa8": {
				"uuids": [
					"ccfac1dd-b3a5-4afd-9fd9-db11aeb65395"
				],
				"occupancy": 1
			}
		},
		"total_channels": 3,
		"total_occupancy": 3
	},
	"service": "Presence"
}

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 where_now() 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 where_now() you can use the following method(s) in the AngularJS SDK:
  1. ParameterTypeRequiredDescription
    callback Function YesCallback is called on receiving where_now response.
    error Function OptionalCallback is called on error to where_now request.
    uuid String OptionalSpecifies the uuid to return channel list for. Default is current uuid.
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.where_now({
    uuid     : 'my_uuid',
    callback : function(m){
        console.log(m)
    },
    error : function(m){
        console.log(m)
    }
});
The where_now() function returns a list of channels a uuid is subscribed to.
  • channels:["String","String", ... ,"String"] - List of channels a uuid is subscribed to.

{
	"channels": [
		"lobby",
		"game01",
		"chat"
	]
}

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
    channel String YesSpecifies the channel for which state is to be set/get.
    channel_groupStringOptionalChannel Group to set the state
    uuid String OptionalThe subscriber uuid to set state for or get current state from. Default is current uuid.
    state Object OptionalJSON 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 Function YesThis callback will be called on completion.
    error Function OptionalThe callback will be called on error.
  1. ParameterTypeRequiredDescription
    channelStringYesSpecifies the channel for which state is to be set/get.
    channel_groupStringOptionalChannel Group to get the state
    uuidStringOptionalThe subscriber uuid to get state for or get current state from. Default is current uuid.
    callbackFunctionYesThis callback will be called on completion.
    errorFunctionOptionalThe callback will be called on error.
// Set state for current uuid.

Pubnub.state({
    channel  : "my_channel",
    state    : {
        "Key": "Value"
    },
    callback : function(m){
        console.log(m)
    },
    error    : function(m){
        console.log(m)
    }
});
// Get state by uuid.
 
Pubnub.state({
	channel  : "my_channel",
	uuid     : "my_uuid",
	callback : function(m){
		console.log(m)
	},
	error    : function(m){
		console.log(m)
	}
});
The state API returns a JSON object containing key value pairs.

{
	first   : "Robert",
	last    : "Plant",
	age     : 59,
	region  : "UK"
}