Presence API for Python-Twisted SDK
Deprecated
NOTICE: Based on current web trends and our own usage data, PubNub's Python Twisted SDK is deprecated as of May 1, 2019. Deprecation means we will no longer be updating the Python Twisted SDK but will continue to support users currently using it. Please feel free to use our other Python SDK offerings as they will continue to be supported and maintained. If you would like to use the Python Twisted SDK specifically, we would love to work with you on keeping this project alive!
Presence lets you track who is online or offline and store custom state information. 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.
This method returns information about the current state of a channel, including a list of unique user IDs (universally unique identifiers, UUIDs) currently subscribed to the channel and the total occupancy count of the channel.
Cache
This method has a 3-second response cache time.
Method(s)
To call Here Now you can use the following method(s) in the Python-Twisted SDK:
1pubnub.here_now().channels(String|List|Tuple).channel_groups(String|List|Tuple).include_state(Boolean).include_uuids(Boolean)
| Parameter | Description |
|---|---|
channelsType: String | List | Tuple Default: n/a | The channels to get the here now details. |
channel_groupsType: String | List | Tuple Default: n/a | The channel groups to get the here now details. |
include_stateType: Boolean Default: False | If True, the response will include the presence states of the users for channels/channelGroups. |
include_uuidsType: Boolean Default: True | If True, the response will include the UUIDs of the connected clients. |
Sample code
Get a list of UUIDs subscribed to channel
1envelope = yield pubnub.here_now()\
2 .channels("my_channel", "demo")\
3 .include_uuids(True)\
4 .future()
5
6if envelope.status.is_error():
7 # handle error
8 return
9
10for channel_data in envelope.result.channels:
11 print("---")
12 print("channel: %s" % channel_data.channel_name)
13 print("occupancy: %s" % channel_data.occupancy)
14
15 print("occupants: %s" % channel_data.channel_name)
show all 17 linesReturns
The here_now() operation returns a PNHereNowResult which contains the following fields:
| Field | Type | Description |
|---|---|---|
total_channels | Int | Total channels. |
total_occupancy | Int | Total occupancy |
channels | Dict | A dict with values of PNHereNowChannelData for each channel. See PNHereNowChannelData for more details. |
PNHereNowChannelData
| Field | Type | Description |
|---|---|---|
channel_name | String | channel name. |
occupancy | Int | occupancy of the channel. |
occupants | List | A list of PNHereNowOccupantData, see PNHereNowOccupantData for more details. |
PNHereNowOccupantData
| Field | Type | Description |
|---|---|---|
uuid | String | uuid of the user. |
state | Dict | state of the user. |
Other examples
Returning state
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
1d = pubnub.here_now().channels("my_channel").\
2 include_uuids(True).include_state(True).deferred()
3
4d.addCallback(my_callback)
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 linesReturn occupancy only
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
You can return only the occupancy information for a single channel by specifying the channel and setting UUIDs to false:
1d = pubnub.here_now().channels("my_channel").\
2 include_uuids(False).include_state(False).deferred()
3
4d.addCallback(my_callback)
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
1d = pubnub.here_now().channel_groups(['cg1', 'cg2', 'cg3']).\
2 include_uuids(True).include_state(True).deferred()
3
4d.addCallback(my_callback)
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 linesWhere now
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
This method returns the list of channels a UUID is subscribed to.
If the app restarts (or the page refreshes) within the heartbeat window, no timeout event is generated.
Method(s)
To call where_now() you can use the following method(s) in the Python-Twisted SDK:
1pubnub.where_now().uuid(String)
| Parameter | Description |
|---|---|
uuidType: String | uuid to get info on. |
Sample code
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 a list of channels a UUID is subscribed to
1# if we're using inlineCallbacks
2envelope = yield pubnub.where_now().deferred()
3
4# if we're not using inlineCallbacks
5d = pubnub.where_now().deferred()
6d.addCallback(my_callback)
Returns
The where_now() operation returns a PNWhereNowResult which contains the following fields:
| Field | Type | Description |
|---|---|---|
channels | List | The list of channels where the UUID is present. |
Other examples
Obtain information about the current list of channels of some other UUID
1d = pubnub.where_now().uuid('some-other-uuid').deferred()
2d.addCallback(my_callback)
User state
Requires Presence
This method requires that the Presence add-on is enabled for your key in the Admin Portal.
Clients can set a dynamic custom state (score, game state, location) for their users on one or more channels and store it on a channel as long as the user stays subscribed.
The state is not persisted, and when the client disconnects, the state data is lost. For more information, refer to Presence State.
Important
Presence state must be expressed as a dict. When calling set_state, be sure to supply an initialized dict which can be serialized.
Method(s)
Set state
1pubnub.set_state().channels(String|List|Tuple).channel_groups(String|List|Tuple).state(Dict)
| Parameter | Description |
|---|---|
channelsType: String | List | Tuple | channels to set state. |
channel_groupsType: String | List | Tuple | channel groups to set state. |
stateType: Dict | state to set. |
Get state
1pubnub.get_state().channels(String|List|Tuple).channel_groups(String|List|Tuple).uuid(String)
| Parameter | Description |
|---|---|
channelsType: String | List | Tuple | channels to get state. |
channel_groupsType: String | List | Tuple | channel groups to get state. |
uuidType: String | uuid to get state from. |
Sample code
Set state
1my_state = {
2 'age': 20
3}
4d = pubnub.set_state().channels(['ch1', 'ch2', 'ch3']).state(my_state).deferred()
5d.addCallback(my_callback)
Get state
1d = pubnub.get_state().channels(['ch1', 'ch2', 'ch3']).uuid('such_uuid').deferred()
2d.addCallback(my_callback)
Returns
- The set_state() operation returns a PNSetStateResult which contains the following fields:
| Field | Type | Description |
|---|---|---|
state | Dict | dict of UUIDs and the user states. |
- The get_state() operation returns a PNGetStateResult which contains the following fields:
| Field | Type | Description |
|---|---|---|
channels | Dict | dict of channels and the user states. |
Other examples
Set state for channels in channel group:
1my_state = {
2 'age': 20
3}
4envelope = yield 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}