Presence API for Python-Asyncio SDK

The Presence API of the PubNub SDK for Python-Asyncio reports which users are online on a channel, which channels a user is on, and the presence state of users. To learn more about Presence as a feature, see the Presence overview.

A builder sends its request when you await its future(). A request that fails, or fails validation, does not raise an exception. future() resolves with a PubNubAsyncioException whose result is None and whose status holds the error. Call is_error() on the awaited value before you read result.

Here now​

here_now() returns the User IDs that are present on channels and channel groups, and the occupancy of each channel, with a PubNub client for Python-Asyncio.

When Access Manager is enabled, calling Here Now requires read permission on the queried channel.

Cache

This method has a 3-second response cache time.

channels#
Type: Union[str, List[str]]
Default: no default
Channel names to return the occupancy and occupants of. When channels and channel_groups are both empty, the SDK requests the occupancy of every channel on the key.
channel_groups#
Type: Union[str, List[str]]
Default: no default
Channel group names to return the occupancy and occupants of. Wildcards are not supported.
include_state#
Type: bool
Default: False
Whether the response includes the presence state of each occupant.
include_uuids#
Type: bool
Default: True
Whether the response lists the User IDs of the occupants of each channel. Set to False to receive only the occupancy counts.
limit#
Type: int
Default: 1000
Here Now accepts a limit of up to 1,000 occupants per channel, and you paginate past that with offset. Setting limit to 0 returns only the occupancy count, without the list of occupants.
offset#
Type: int
Default: no default
Zero-based position of the first occupant to return. Use it with limit to page through the occupants of a channel. The SDK sends offset only when it is set.

The keyword arguments of here_now() are channels, channel_groups, include_state and include_uuids. Set limit and offset with the builder methods.

Returns​

here_now() resolves with an AsyncioEnvelope whose result is a PNHereNowResult with the total channel count, the total occupancy, and the occupants of each channel, and whose status is a PNStatus.

PNHereNowResult​

total_channels#
Type: int
Number of channels in the response.
total_occupancy#
Type: int
Total number of occupants across those channels.
channels#
Type: List[PNHereNowChannelData]
One PNHereNowChannelData for each channel.

PNHereNowChannelData​

channel_name#
Type: str
Name of the channel.
occupancy#
Type: int
Number of occupants of the channel.
occupants#
Type: List[PNHereNowOccupantsData]
The PNHereNowOccupantsData of each occupant. Empty or None when include_uuids is False.

PNHereNowOccupantsData​

uuid#
Type: str
User ID of the occupant.
state#
Type: dict
Presence state of the occupant. None when the occupant has no state or include_state is False.

Syntax

1await pubnub.here_now() \
2 .channels(channels: Union[str, List[str]]) \
3 .channel_groups(channel_groups: Union[str, List[str]]) \
4 .include_state(include_state: bool) \
5 .include_uuids(include_uuids: bool) \
6 .limit(limit: int) \
7 .offset(offset: int) \
8 .future() -> AsyncioEnvelope

Where now​

where_now() returns the channels that a User ID is present on, with a PubNub client for Python-Asyncio.

Where Now requires no channel-level permission when Access Manager is enabled because it looks up a User ID rather than a channel.

uuid#
Type: str
Default: no default
User ID to list the channels of. When omitted, the SDK uses the user_id of the PubNub client. The keyword argument of where_now() is user_id.

Returns​

where_now() resolves with an AsyncioEnvelope whose result is a PNWhereNowResult with the IDs of the channels where the user is present, and whose status is a PNStatus.

PNWhereNowResult​

channels#
Type: List[str]
The channels where the User ID is present.

Syntax

1await pubnub.where_now() \
2 .uuid(uuid: str) \
3 .future() -> AsyncioEnvelope

Set state​

set_state() sets the presence state of the current User ID of a PubNub client for Python-Asyncio on channels and channel groups.

Presence state is custom data, such as a score, a game state, or a location. PubNub holds it for a User ID on a channel while the User ID stays subscribed. PubNub doesn't persist presence state, and disconnecting a client clears it. Setting presence state overwrites the entire existing state of the User ID on that channel, so send the full state object, not only the field that changed. To learn more, see Set and get presence state.

When Access Manager is enabled, Get State and Set State both require read permission on the channel.

required
channels required#
Type: Union[str, List[str]]
Default: no default
Channel names to set the presence state on. The SDK refuses a call that has channel_groups and no channels, so always provide channels. A call with neither fails validation with the message Channel or group missing.
channel_groups#
Type: Union[str, List[str]]
Default: no default
Channel group names to set the presence state on. Wildcards are not supported. A call with channel_groups and no channels fails validation with the message State setter for channel groups is not supported yet.
state required#
Type: Dict[str, Any]
Default: no default
Presence state to set, as a JSON-serializable object. The SDK accepts only a dict. A call with state missing or not a dict fails validation with the message State missing or not a dict.

The keyword arguments of set_state() are channels, channel_groups and state.

Returns​

set_state() resolves with an AsyncioEnvelope whose result is a PNSetStateResult with the state that PubNub stored for the user, and whose status is a PNStatus.

PNSetStateResult​

state#
Type: dict
The presence state that PubNub stored for the User ID.

Syntax

1await pubnub.set_state() \
2 .channels(channels: Union[str, List[str]]) \
3 .channel_groups(channel_groups: Union[str, List[str]]) \
4 .state(state: Dict[str, Any]) \
5 .future() -> AsyncioEnvelope

Get state​

get_state() reads the presence state of a User ID on channels and channel groups, using a PubNub client for Python-Asyncio.

Presence state is custom data, such as a score, a game state, or a location. PubNub holds it for a User ID on a channel while the User ID stays subscribed. PubNub doesn't persist presence state, and disconnecting a client clears it. To learn more, see Set and get presence state.

When Access Manager is enabled, Get State and Set State both require read permission on the channel.

channels#
Type: Union[str, List[str]]
Default: no default
Channel names to read the presence state from. Provide channels, channel_groups, or both. A call with neither fails validation with the message Channel or group missing.
channel_groups#
Type: Union[str, List[str]]
Default: no default
Channel group names to read the presence state from. Wildcards are not supported.
uuid#
Type: str
Default: no default
User ID to read the presence state of. When omitted, the SDK uses the user_id of the PubNub client.

The keyword arguments of get_state() are channels, channel_groups and uuid.

Returns​

get_state() resolves with an AsyncioEnvelope whose result is a PNGetStateResult with the state of the user, grouped by channel, and whose status is a PNStatus.

PNGetStateResult​

channels#
Type: Dict[str, dict]
The presence state of the User ID, keyed by channel name.

Syntax

1await pubnub.get_state() \
2 .channels(channels: Union[str, List[str]]) \
3 .channel_groups(channel_groups: Union[str, List[str]]) \
4 .uuid(uuid: str) \
5 .future() -> AsyncioEnvelope

Receive presence events​

with_presence=True makes a subscription in the Python-Asyncio SDK deliver join, leave, timeout, interval and state-change events to its on_presence callback.

Presence events require the Presence add-on to be enabled for your key. The callback receives a PNPresenceEventResult with the attributes event, channel, subscription, timetoken, timestamp, uuid, occupancy, state, join, leave and timeout. The event attribute holds the action.

Events for join, leave and timeout carry uuid and occupancy. A state-change event carries uuid and state. An interval event carries occupancy and the optional lists join, leave and timeout.

Up to the Announce Max threshold, a channel emits individual join, leave, and timeout events. Above it, the channel emits periodic interval events instead, while state-change stays individual in both modes.

To learn more about subscriptions and event listeners, see Publish & Subscribe.

Basic usage

1def on_presence(listener):
2 def presence_callback(presence):
3 print(f"\033[0;32mPresence received on: {listener}: \t{presence.uuid} {presence.event}s "
4 f"{presence.subscription or presence.channel}\033[0m")
5 return presence_callback
6
7subscription = pubnub.channel("my_channel").subscription(with_presence=True)
8subscription.on_presence = on_presence('presence_listener')
9
10subscription.subscribe()

Was this page useful?

Last updated on