AsyncioAsyncioTwistedTornadoNativePythonPython-aiohttp V4 Presence API Reference for Realtime Apps

Go to Publish & Subscribe


 

These docs are for PubNub 4.0 for Python-aiohttp V4 which is our latest and greatest! For the docs of the older versions of the SDK, please check PubNub 3.0 for Python, PubNub 3.0 for Python-Tornado and PubNub 3.0 for Python-Twisted.

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

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 Python-aiohttp V4 SDK:
  1. ParameterTypeRequiredDefaultsDescription
    channelsString | List | TupleOptionalThe channels to get the here now details.
    channel_groupsString | List | TupleOptionalThe channel groups to get the here now details.
    include_stateBooleanOptionalFalseIf True, the response will include the presence states of the users for channels/channelGroups.
    include_uuidsBooleanOptionalTrueIf True, the response will include the UUIDs of the connected clients.
envelope = await pubnub.here_now()\
        .channels(['cool_channel1', 'cool_channel2'])\
        .include_uuids(True)\
        .future()

if envelope.status.is_error():
    # handle error
    return

for channel_data in envelope.result.channels:
    print("---")
    print("channel: %s" % channel_data.channel_name)
    print("occupancy: %s" % channel_data.occupancy)

    print("occupants: %s" % channel_data.channel_name)
for occupant in channel_data.occupants:
    print("uuid: %s, state: %s" % (occupant.uuid, occupant.state))

The here_now() operation returns a PNHereNowResult which contains the following fields:

FieldTypeDescription
total_channelsIntTotal channels.
total_occupancyIntTotal occupancy
channelsDictA dict with values of PNHereNowChannelData for each channel. See PNHereNowChannelData for more details.
FieldTypeDescription
channel_nameStringchannel name.
occupancyIntoccupancy of the channel.
occupantsListA list of PNHereNowOccupantData, see PNHereNowOccupantData for more details.
FieldTypeDescription
uuidStringuuid of the user.
stateDictstate of the user.
  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.
    envelope = await pubnub.here_now().channels("my_channel").\
        include_uuids(True).include_state(True).future()
    {
        total_channels: 1,
        channels: [{
            channel_name: "my_channel",
            occupancy: 1,
            occupants: [{
                uuid: "myUuid1"
                state: {
                    "abcd": {
                        "age": 15
                    }
                }    
            }]
        }],
        total_occupancy: 1
    }
  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:
    envelope = await pubnub.here_now().channels("my_channel").\
        include_uuids(False).include_state(False).future()
    {
        total_channels: 1,
        channels: [{
            channel_name: "my_channel",
            occupancy: 3,
            occupants: []
        }],
        total_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:
    envelope = await pubnub.here_now().include_uuids(True).include_state(False).future()
    {
        total_channels: 2,
        channels: [
            {
                channel_name: "my_channel",
                occupancy: 1,
                occupants: [{
                    uuid: "data01",
                    state: None
                }]
            },
            {
                occupancy: 2,
                occupants: [{
                    uuid: "jason01",
                    state: None
                }]
            }
        ],
        total_occupancy: 3
    }
  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
    envelope = await pubnub.here_now()\
        .include_state(True)\
        .include_uuids(True)\
        .future()
    {
        total_channels: 1,
        channels: [{
            channel_name: "my_channel",
            occupancy: 3,
            occupants: []
        }],
        total_occupancy: 3
    }
  5. envelope = await pubnub.here_now().channel_groups(['cg1', 'cg2', 'cg3']).\
        include_uuids(True).include_state(True).future()
    {
        total_channels: 1,
        channels: [
            {
                channel_name: "my_channel",
                occupancy: 1,
                occupants: [{
                    uuid: "143r34f34t34fq34q34q3",
                    state: None
                }]
            },
            {
                occupancy: 1,
                occupants: [{
                    uuid: "123123234t234f34fq3dq",
                    state: None
                }]
            },
            {
                occupancy: 1,
                occupants: [{
                    uuid: "23f34d3f4rq34r34rq23q",
                    state: None
                }]
            },
            {
                occupancy: 1,
                occupants: [{
                    uuid: "w34tcw45t45tcw435tww3",
                    state: None
                }]
            }
        ],
        total_occupancy: 4
    }
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 Python-aiohttp V4 SDK:
  1. ParameterTypeRequiredDescription
    uuidStringOptionaluuid to get info on.
You simply need to define the uuid and the callback function to be used to send the data to as in the example below.
envelope = await pubnub.where_now().future()
The where_now() operation returns a PNWhereNowResult which contains the following fields:
FieldTypeDescription
channelsListThe list of channels where the UUID is present.
envelope = await pubnub.where_now().uuid('some-other-uuid').future()
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.
 
Presence state must be expressed as a dict. When calling set_state, be sure to supply an initialized dict which can be serialized.
  1. ParameterTypeRequiredDescription
    channelsString | List | TupleOptionalchannels to set state.
    channel_groupsString | List | TupleOptionalchannel groups to set state.
    stateDictOptionalstate to set.
  1. ParameterTypeRequiredDescription
    channelsString | List | TupleOptionalchannels to get state.
    channel_groupsString | List | TupleOptionalchannel groups to get state.
    uuidStringOptionaluuid to get state on.
my_state = {
    'age': 20
}
envelope = await pubnub.set_state().channels(['ch1', 'ch2', 'ch3']).state(my_state).future()
envelope = await pubnub.get_state().channels(['ch1', 'ch2', 'ch3']).uuid('such_uuid').future()
FieldTypeDescription
stateDictdict of UUIDs and the user states.
FieldTypeDescription
channelsDictdict of channels and the user states.
  1. my_state = {
        'age': 20
    }
    envelope = await pubnub.set_state().channel_groups(['gr1', 'gr2', 'gr3']).state(my_state).future()
    The above code would return the following response to the client:
    {
    	first  : "Robert",
    	last   : "Plant",
    	age    : 59,
    	region : "UK"
    }

Go to Access Manager