Presence API for Python-Asyncio SDK

Presence lets you track which users and devices are online and store custom state. Presence shows:

  • When a user has joined or left a channel
  • How many users are subscribed to a particular channel (occupancy)
  • Which channels a user or device is subscribed to
  • Presence state associated with 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.

For information on how to receive presence events and what those events are, refer to Presence Events.

Use here_now() to get who is on a channel (UUIDs) and how many users are subscribed (occupancy).

Cache

This method has a 3-second response cache time.

Method(s)​

Use the following method in the Python SDK:

1pubnub.here_now() \
2 .channels(String|List|Tuple) \
3 .channel_groups(String|List|Tuple) \
4 .include_state(Boolean) \
5 .include_uuids(Boolean)
* required
ParameterDescription
channels
Type: String | List | Tuple
Default:
None
Channels to query for details.
channel_groups
Type: String | List | Tuple
Default:
None
Channel groups to query for details. Wildcards aren't supported.
include_state
Type: Boolean
Default:
False
Whether the response should include the presence state for users on channels and channel groups.
include_uuids
Type: Boolean
Default:
True
Whether the response should include the UUIDs of connected clients.

Sample code​

Get a list of UUIDs subscribed to channel​

Reference code
This example is a self-contained code snippet ready to be run. It includes necessary imports and executes methods with console logging. Use it as a reference when working with other examples in this document.
1import asyncio
2import os
3from pubnub.pnconfiguration import PNConfiguration
4from pubnub.pubnub_asyncio import PubNubAsyncio
5from pubnub.exceptions import PubNubException
6
7
8async def get_here_now(pubnub: PubNubAsyncio):
9 try:
10 # Get a list of UUIDs subscribed to channels
11 envelope = await pubnub.here_now() \
12 .channels(['cool_channel1', 'cool_channel2']) \
13 .include_uuids(True) \
14 .future()
15
show all 46 lines

Returns​

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

FieldTypeDescription
total_channelsIntTotal number of channels.
total_occupancyIntTotal occupancy count across all channels.
channelsDictionaryDictionary mapping channel names (String) to PNHereNowChannelData objects.

PNHereNowChannelData​

FieldTypeDescription
channel_nameStringChannel name.
occupancyIntOccupancy of the channel.
occupantsListList of PNHereNowOccupantData objects.

PNHereNowOccupantData​

FieldTypeDescription
uuidStringUUID of the user.
stateDictionaryPresence state for the user.

Returning state​

Requires Presence

This method requires that the Presence add-on is enabled for your key in the Admin Portal.

For information on how to receive presence events and what those events are, refer to Presence Events.

1envelope = await pubnub.here_now() \
2 .channels("my_channel") \
3 .include_uuids(True) \
4 .include_state(True) \
5 .future()
Example response​
1{
2 total_channels: 1,
3 channels: [{
4 channel_name: "my_channel",
5 occupancy: 1,
6 occupants: [{
7 uuid: "myUuid1"
8 state: {
9 "abcd": {
10 "age": 15
11 }
12 }
13 }]
14 }],
15 total_occupancy: 1
show all 16 lines

Return occupancy only​

Requires Presence

This method requires that the Presence add-on is enabled for your key in the Admin Portal.

For information on how to receive presence events and what those events are, refer to Presence Events.

To return only occupancy for a channel, set include_uuids and include_state to False:

1envelope = await pubnub.here_now() \
2 .channels("my_channel") \
3 .include_uuids(False) \
4 .include_state(False) \
5 .future()
Example response​
1{
2 total_channels: 1,
3 channels: [{
4 channel_name: "my_channel",
5 occupancy: 3,
6 occupants: []
7 }],
8 total_occupancy: 3
9}

Here now for channel groups​

1envelope = await pubnub.here_now() \
2 .channel_groups(['cg1', 'cg2', 'cg3']) \
3 .include_uuids(True) \
4 .include_state(True) \
5 .future()
Example response​
1{
2 total_channels: 1,
3 channels: [
4 {
5 channel_name: "my_channel",
6 occupancy: 1,
7 occupants: [{
8 uuid: "143r34f34t34fq34q34q3",
9 state: None
10 }]
11 },
12 {
13 occupancy: 1,
14 occupants: [{
15 uuid: "123123234t234f34fq3dq",
show all 35 lines

Where now​

Requires Presence

This method requires that the Presence add-on is enabled for your key in the Admin Portal.

For information on how to receive presence events and what those events are, refer to Presence Events.

Use where_now() to get the list of channels a UUID is subscribed to.

Timeout events

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 Python SDK:

1pubnub.where_now() \
2 .uuid(String)
* required
ParameterDescription
uuid
Type: String
uuid to get info on.

Sample code​

1envelope = await pubnub.where_now() \
2 .future()

Returns​

The where_now() operation returns a PNWhereNowResult which contains the following fields:

FieldTypeDescription
channelsListThe list of channels where the UUID is present.

Other examples​

Obtain information about the current list of channels of some other UUID​

1envelope = await pubnub.where_now() \
2 .uuid('some-other-uuid') \
3 .future()

User state​

Requires Presence

This method requires that the Presence add-on is enabled for your key in the Admin Portal.

For information on how to receive presence events and what those events are, refer to Presence Events.

Clients can set a dynamic custom state (for example, score, game state, or location) for their users on one or more channels. The state exists while the user stays subscribed.

The state isn't persisted. When the client disconnects, the state is lost. For more information, refer to Presence State.

Presence state format

Presence state must be a dict. When calling set_state, supply an initialized, serializable dict.

Method(s)​

Set state​

1pubnub.set_state() \
2 .channels(String|List|Tuple) \
3 .channel_groups(String|List|Tuple) \
4 .state(Dictionary)
* required
ParameterDescription
channels
Type: String | List | Tuple
channels to set state.
channel_groups
Type: String | List | Tuple
channel groups to set state.
state
Type: Dictionary
state to set.

Get state​

1pubnub.get_state() \
2 .channels(String|List|Tuple) \
3 .channel_groups(String|List|Tuple) \
4 .uuid(String)
* required
ParameterDescription
channels
Type: String | List | Tuple
channels to get state.
channel_groups
Type: String | List | Tuple
channel groups to get state.
uuid
Type: String
uuid to get state from.

Sample code​

Set state​

1my_state = {
2 'age': 20
3}
4envelope = await pubnub.set_state() \
5 .channels(['ch1', 'ch2', 'ch3']) \
6 .state(my_state) \
7 .future()

Get state​

1envelope = await pubnub.get_state() \
2 .channels(['ch1', 'ch2', 'ch3']) \
3 .uuid('such_uuid') \
4 .future()

Returns​

The set_state() operation returns a PNSetStateResult which contains the following fields​

FieldTypeDescription
stateDictionaryDictionary of UUIDs and the user states.

The get_state() operation returns a PNGetStateResult which contains the following fields​

FieldTypeDescription
channelsDictionaryDictionary of channels and the user states.

Other examples​

Set state for channels in channel group​

1my_state = {
2 'age': 20
3}
4envelope = await pubnub.set_state().channel_groups(['gr1', 'gr2', 'gr3']).state(my_state).future()

The above code would return the following response to the client:

1{
2 first : "Robert",
3 last : "Plant",
4 age : 59,
5 region : "UK"
6}

Was this page useful?

Last updated on