SelectParse Presence API Reference for Realtime Apps

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 Parse 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({
    	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"
    }
  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 Parse SDK:
  1. You can call Global Here Now by using the same method as Here Now
pubnub.here_now({
	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"
}

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 Parse 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"
	]
}