App Context API for Python SDK

This page describes App Context (formerly Objects v2). To upgrade from Objects v1, refer to the migration guide.

App Context provides easy-to-use, serverless storage for user and channel data you need to build innovative, reliable, scalable applications. Use App Context to store metadata about your application users and channels, and their membership associations, without the need to stand up your own databases.

PubNub also triggers events when object data is changed: set, updated, or removed from the database. Making a request to set the same data that already exists doesn't trigger an event. Clients can receive these events in real time and update their front-end application accordingly.

Request execution and return values

You can decide whether to perform the Python SDK operations synchronously or asynchronously.

  • .sync() returns an Envelope object, which has two fields: Envelope.result, whose type differs for each API, and Envelope.status of type PnStatus.

    1pubnub.publish() \
    2 .channel("myChannel") \
    3 .message("Hello from PubNub Python SDK") \
    4 .sync()
  • .pn_async(callback) returns None and passes the values of Envelope.result and Envelope.status to a callback you must define beforehand.

    1def my_callback_function(result, status):
    2 print(f'TT: {result.timetoken}, status: {status.category.name}')
    3
    4pubnub.publish() \
    5 .channel("myChannel") \
    6 .message("Hello from PubNub Python SDK") \
    7 .pn_async(my_callback_function)

User​

Manage UUID metadata: list, fetch, set, and remove. Keep responses small by including only the fields you need.

Get metadata for all users​

Get a paginated list of UUID metadata. Use filters and sorting to narrow results.

Required keyset configuration
To get all channel and user metadata, you must uncheck the Disallow Get All Channel Metadata and Disallow Get All User Metadata checkboxes in the App Context section of your keyset configuration in the Admin Portal.

Method(s)​

To Get All UUID Metadata you can use the following method(s) in the Python SDK:

1pubnub.get_all_uuid_metadata() \
2 .limit(Integer) \
3 .page(PNPage Object) \
4 .filter(String) \
5 .sort(List<PNSortKey>) \
6 .include_total_count(Boolean) \
7 .include_custom(Boolean) \
8 .include_status(Boolean) \
9 .include_type(Boolean)
limit#
Type: Integer
Default: no default
The maximum number of objects to retrieve at a time.
page#
Type: PNPage
Default: no default
The paging object used for pagination.
filter#
Type: String
Default: no default
Expression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined in the filtering reference.
sort#
Type: List<PNSortKey>
Default: no default
List of properties to sort by. Available options are id, name, and updated. Use asc or desc to specify sort direction. For example: {name: 'asc'}.
include_total_count#
Type: Boolean
Default: False
Whether to include the total count in the paginated response. Default is false.
include_custom#
Type: Boolean
Default: False
Whether to include the Custom object in the response.
include_status#
Type: Boolean
Default: True
Whether to include the status field in the fetch response. Setting this to False will prevent this value from being returned.
include_type#
Type: Boolean
Default: True
Whether to include the type field in the fetch response. Setting this to False will prevent this value from being returned.

Sample code​

Synchronous:

1import os
2from pubnub.pnconfiguration import PNConfiguration
3from pubnub.pubnub import PubNub
4from pubnub.models.consumer.objects_v2.sort import PNSortKey, PNSortKeyValue
5from pubnub.exceptions import PubNubException
6
7
8def get_all_uuid_metadata(pubnub: PubNub):
9 try:
10 result = pubnub.get_all_uuid_metadata() \
11 .include_custom(True) \
12 .limit(10) \
13 .include_total_count(True) \
14 .sort(PNSortKey.asc(PNSortKeyValue.ID), PNSortKey.desc(PNSortKeyValue.UPDATED)) \
15 .page(None) \
show all 43 lines

Asynchronous:

1import os
2from pubnub.pnconfiguration import PNConfiguration
3from pubnub.pubnub import PubNub
4from pubnub.models.consumer.objects_v2.sort import PNSortKey, PNSortKeyValue
5
6
7def callback(response, status):
8 if status.is_error():
9 print(f"Error: {status.error_data}")
10 else:
11 for uuid_data in response.data:
12 print(f"UUID: {uuid_data["id"]}")
13 print(f"Name: {uuid_data["name"]}")
14 print(f"Custom: {uuid_data["custom"]}")
15
show all 43 lines
Returns​

The get_all_uuid_metadata() operation returns an Envelope which contains the following fields:

FieldTypeDescription
resultPNGetAllUUIDMetadataResultA detailed object containing the result of the operation.
statusPNStatusA status object with additional information.

PNGetAllUUIDMetadataResult​

Property NameTypeDescription
data[]List of dictionaries containing UUID metadata
statusPNStatusStatus of the operation

Each element in data contains a dictionary with UUID metadata.

KeyDescription
idUUID
nameName associated with UUID object
externalIdExternal ID associated with UUID object
profileUrlProfile URL associated with UUID object
emailEmail address associated with UUID object
customCustom object associated with UUID object in form of dictionary containing string to string pairs
statusUser status value
typeUser type value

Get user metadata​

Fetch metadata for a single UUID. Include the Custom object if you need custom fields.

Method(s)​

To Get UUID Metadata you can use the following method(s) in the Python SDK:

1pubnub.get_uuid_metadata() \
2 .uuid(String) \
3 .include_custom(Boolean)
uuid#
Type: String
Default: pubnub.configuration.uuid
Unique UUID Metadata identifier.
If not supplied, then UUID from configuration will be used.
include_custom#
Type: Boolean
Default: False
Whether to include the Custom object in the response.
include_status#
Type: Boolean
Default: True
Whether to include the status field in the fetch response, which is included by default.
include_type#
Type: Boolean
Default: True
Whether to include the type field in the fetch response, which is included by default.

Sample code​

Synchronous:

1pubnub.get_uuid_metadata() \
2 .include_custom(True) \
3 .sync()

Asynchronous:

1def callback(response, status):
2 pass
3
4pubnub.get_uuid_metadata() \
5 .include_custom(True) \
6 .pn_async(callback)
Returns​

The get_uuid_metadata() operation returns an Envelope which contains the following fields:

FieldTypeDescription
resultPNGetUUIDMetadataResultA detailed object containing the result of the operation.
statusPNStatusA status object with additional information.
PNGetUUIDMetadataResult​

operation returns a PNGetUUIDMetadataResult which contains the following properties:

Property NameTypeDescription
dataDictionary containing UUID metadata
statusPNStatusStatus of the operation

Where each element in data contains a dictionary with UUID metadata.

KeyDescription
idUUID
nameName associated with UUID object
externalIdExternal ID associated with UUID object
profileUrlProfile URL associated with UUID object
emailEmail address associated with UUID object
statusStatus value associated with UUID object
typeType value associated with UUID object
customCustom object associated with UUID object in form of dictionary containing string to string pairs

Set user metadata​

Create or update metadata for a UUID. Use the eTag to avoid overwriting concurrent updates.

Unsupported partial updates of custom metadata

The value of the custom metadata parameter sent in this method always overwrites the value stored on PubNub servers. If you want to add new custom data to an existing one, you must:

  1. Get the existing metadata and store it locally.
  2. Append the new custom metadata to the existing one.
  3. Set the entire updated custom object.

Method(s)​

To Set UUID Metadata you can use the following method(s) in the Python SDK:

1pubnub.set_uuid_metadata() \
2 .uuid(String) \
3 .set_name(String) \
4 .set_status(String) \
5 .set_type(String) \
6 .external_id(String) \
7 .profile_url(String) \
8 .email(String) \
9 .custom(Dictionary) \
10 .include_custom(Boolean) \
11 .include_status(Boolean) \
12 .include_type(Boolean) \
13 .if_matches_etag(String)
uuid#
Type: String
Default: pubnub.configuration.uuid
Unique UUID Metadata identifier.
If not supplied, then UUID from configuration will be used.
set_name#
Type: String
Default: no default
Display name for the user.
set_status#
Type: String
Default: no default
User status. Max. 50 characters.
set_type#
Type: String
Default: no default
User type. Max. 50 characters.
external_id#
Type: String
Default: no default
User's identifier in an external system.
profile_url#
Type: String
Default: no default
The URL of the user's profile picture.
email#
Type: String
Default: no default
The user's email address.
custom#
Type: Any
Default: no default
Any object of key-value pairs with supported data types. App Context filtering language doesn't support filtering by custom properties.
include_custom#
Type: Boolean
Default: False
Whether to include the Custom object in the response.
include_status#
Type: Boolean
Default: False
Whether to include the status object in the fetch response.
include_type#
Type: Boolean
Default: False
Whether to include the type object in the fetch response.
if_matches_etag#
Type: String
Default: no default
The entity tag to be used to ensure updates only happen if the object hasn't been modified since it was read. Use the eTag you received from an applicable get metadata method to check against the server entity tag. If the eTags don't match, an HTTP 412 error is thrown.
API limits

To learn about the maximum length of parameters used to set user metadata, refer to REST API docs.

Sample code​

Synchronous:

1pubnub.set_uuid_metadata() \
2 .include_custom(True) \
3 .uuid("Some UUID") \
4 .set_name("Some Name") \
5 .set_status("Active") \
6 .set_type("User") \
7 .email("test@example.com") \
8 .profile_url("http://example.com") \
9 .external_id("1234567890") \
10 .custom({"key1": "val1", "key2": "val2"}) \
11 .sync()

Asynchronous:

1def callback(response, status):
2 pass
3
4pubnub.set_uuid_metadata() \
5 .include_custom(True) \
6 .uuid("Some UUID") \
7 .set_name("Some Name") \
8 .set_status("Active") \
9 .set_type("User") \
10 .email("test@example.com") \
11 .profile_url("http://example.com") \
12 .external_id("1234567890") \
13 .custom({"key1": "val1", "key2": "val2"}) \
14 pn_async(callback)
Returns​

The set_uuid_metadata() returns a PNSetUUIDMetadataResult which contains the following properties:

Property NameTypeDescription
dataDictionary containing UUID metadata
statusPNStatusStatus of the operation

Where each element in data contains a dictionary with UUID metadata.

KeyDescription
idUUID
nameName associated with UUID object
externalIdExternal ID associated with UUID object
profileUrlProfile URL associated with UUID object
emailEmail address associated with UUID object
statusUser status
typeUser type
customCustom object associated with UUID object in form of dictionary containing string to string pairs

Remove user metadata​

Delete metadata for the specified UUID.

Method(s)​

To Remove UUID Metadata you can use the following method(s) in the Python SDK:

1pubnub.remove_uuid_metadata() \
2 .uuid(String)
uuid#
Type: String
Default: pubnub.configuration.uuid
Unique UUID Metadata identifier.
If not supplied, then UUID from configuration will be used.

Sample code​

Synchronous:

1pubnub.remove_uuid_metadata() \
2 .uuid("Some UUID").sync()

Asynchronous:

1def callback(response, status):
2 pass
3
4pubnub.remove_uuid_metadata() \
5 .uuid("Some UUID").pn_async(callback)
Returns​

The remove_uuid_metadata() operation returns an Envelope which contains the following fields:

FieldTypeDescription
resultPNRemoveUUIDMetadataResultA detailed object containing the result of the operation.
statusPNStatusA status object with additional information.
PNRemoveUUIDMetadataResult​
Property NameTypeDescription
statusPNStatusStatus of the operation

Channel​

Manage channel metadata: list, fetch, set, and remove.

Get metadata for all channels​

Get a paginated list of channel metadata. Use filters and sorting to narrow results.

Required keyset configuration
To get all channel and user metadata, you must uncheck the Disallow Get All Channel Metadata and Disallow Get All User Metadata checkboxes in the App Context section of your keyset configuration in the Admin Portal.

Method(s)​

To Get All Channel Metadata you can use the following method(s) in the Python SDK:

1pubnub.get_all_channel_metadata() \
2 .limit(Integer) \
3 .page(PNPage) \
4 .filter(String) \
5 .sort(PNSortKey) \
6 .include_total_count(Boolean) \
7 .include_custom(Boolean) \
8 .include_status(Boolean) \
9 .include_type(Boolean)
limit#
Type: Integer
Default: 100
The maximum number of objects to retrieve at a time.
page#
Type: PNPage
Default: no default
The paging object used for pagination.
filter#
Type: String
Default: no default
Expression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined in the filtering reference.
sort#
Type: [PNSortKey]
Default: no default
List of properties to sort by. Available options are id, name, and updated. Use asc or desc to specify sort direction. For example: {name: 'asc'}.
include_total_count#
Type: Boolean
Default: False
Whether to include the total count in the paginated response. Default is false.
include_custom#
Type: Boolean
Default: False
Whether to include the Custom object in the response.
include_status#
Type: Boolean
Default: True
Whether to include the status field in the fetch response. Setting this to False will prevent this value from being returned.
include_type#
Type: Boolean
Default: True
Whether to include the type field in the fetch response. Setting this to False will prevent this value from being returned.

Sample code​

Synchronous:

1pubnub.get_all_channel_metadata() \
2 .include_custom(True) \
3 .limit(10) \
4 .include_total_count(True) \
5 .sort(PNSortKey.asc(PNSortKeyValue.ID), PNSortKey.desc(PNSortKeyValue.UPDATED)) \
6 .page(None) \
7 .sync()

Asynchronous:

1def callback(response, status):
2 pass
3
4pubnub.get_all_channel_metadata() \
5 .include_custom(True) \
6 .limit(10) \
7 .include_total_count(True) \
8 .sort(PNSortKey.asc(PNSortKeyValue.ID), PNSortKey.desc(PNSortKeyValue.UPDATED)) \
9 .page(None) \
10 .pn_async(callback)

Returns​

The get_all_channel_metadata() operation returns an Envelope which contains the following fields:

FieldTypeDescription
resultPNGetAllChannelMetadataResultA detailed object containing the result of the operation.
statusPNStatusA status object with additional information.
PNGetAllChannelMetadataResult​
Property NameTypeDescription
data[]List of dictionaries containing channel metadata
statusPNStatusStatus of the operation

Where each element in data contains a dictionary with channel metadata.

KeyDescription
idChannel metadata ID
nameName associated with channel metadata object
descriptionDescription associated with channel metadata object
statusChannel status value
typeChannel type value
customCustom object associated with channel metadata object in form of dictionary containing string to string pairs

Get channel metadata​

Fetch metadata for a single channel. Include the Custom object if you need custom fields.

Method(s)​

To Get Channel Metadata you can use the following method(s) in the Python SDK:

1pubnub.get_channel_metadata() \
2 .channel(String) \
3 .include_custom(Boolean) \
4 .include_status(Boolean) \
5 .include_type(Boolean)
channel#
Type: str
Default: no default
Channel name
include_custom#
Type: bool
Default: False
Whether to include the custom object in the fetch response.
include_status#
Type: Boolean
Default: True
Whether to include the status field in the fetch response. Setting this to False will prevent this value from being returned.
include_type#
Type: Boolean
Default: True
Whether to include the type field in the fetch response. Setting this to False will prevent this value from being returned.

Sample code​

Synchronous:

1pubnub.get_channel_metadata() \
2 .include_custom(True) \
3 .channel("channel") \
4 .sync()

Asynchronous:

1def callback(response, status):
2 pass
3
4pubnub.get_channel_metadata() \
5 .include_custom(True) \
6 .channel("channel") \
7 .pn_async(callback)
Returns​

The get_channel_metadata() operation returns an Envelope which contains the following fields:

FieldTypeDescription
resultPNGetChannelMetadataResultA detailed object containing the result of the operation.
statusPNStatusA status object with additional information.
PNGetChannelMetadataResult​
Property NameTypeDescription
dataDictionary containing channel metadata
statusPNStatusStatus of the operation

Where each element in data contains a dictionary with channel metadata.

KeyDescription
idChannel metadata ID
nameName associated with channel metadata object
descriptionDescription associated with channel metadata object
statusChannel status value
typeChannel type value
customCustom object associated with channel metadata object in form of dictionary containing string to string pairs

Set channel metadata​

Create or update metadata for a channel. Use the eTag to avoid overwriting concurrent updates.

Set metadata for a channel in the database, optionally including its custom data object.

Unsupported partial updates of custom metadata

The value of the custom metadata parameter sent in this method always overwrites the value stored on PubNub servers. If you want to add new custom data to an existing one, you must:

  1. Get the existing metadata and store it locally.
  2. Append the new custom metadata to the existing one.
  3. Set the entire updated custom object.

Method(s)​

To Set Channel Metadata you can use the following method(s) in the Python SDK:

1pubnub.set_channel_metadata() \
2 .channel(String) \
3 .set_name(String) \
4 .set_status(String) \
5 .set_type(String) \
6 .description(String) \
7 .custom(Dictionary) \
8 .include_custom(Boolean) \
9 .include_status(Boolean) \
10 .include_type(Boolean) \
11 .if_matches_etag(String)
channel#
Type: String
Default: no default
Channel ID.
set_name#
Type: String
Default: no default
Name of the channel.
set_status#
Type: String
Default: no default
Channel status. Max. 50 characters.
set_type#
Type: String
Default: no default
Channel type. Max. 50 characters.
description#
Type: String
Default: no default
Description of a channel.
custom#
Type: Map<String, Object>
Default: no default
Any object of key-value pairs with supported data types. App Context filtering language doesn't support filtering by custom properties.
include_custom#
Type: Boolean
Default: False
Whether to include the custom object in the fetch response.
include_status#
Type: Boolean
Default: False
Whether to include the status object in the fetch response.
include_type#
Type: Boolean
Default: False
Whether to include the type object in the fetch response.
if_matches_etag#
Type: String
Default: no default
The entity tag to be used to ensure updates only happen if the object hasn't been modified since it was read. Use the eTag you received from an applicable get metadata method to check against the server entity tag. If the eTags don't match, an HTTP 412 error is thrown.
API limits

To learn about the maximum length of parameters used to set channel metadata, refer to REST API docs.

Sample code​

Synchronous:

1pubnub.set_channel_metadata() \
2 .include_custom(True) \
3 .channel("channel id") \
4 .set_name("Channel Name") \
5 .set_status("Archived") \
6 .set_type("Archived") \
7 .description("Description") \
8 .custom({ "key1": "val1", "key2": "val2" }) \
9 .sync()

Asynchronous:

1def callback(response, status):
2 pass
3
4pubnub.set_channel_metadata() \
5 .include_custom(True) \
6 .channel("channel id") \
7 .set_name("Channel Name") \
8 .set_status("Archived") \
9 .set_type("Archived") \
10 .description("Description") \
11 .custom({ "key1": "val1", "key2": "val2" }) \
12 .pn_async(callback)
Returns​

The set_channel_metadata() operation returns an Envelope which contains the following fields:

FieldTypeDescription
resultPNSetChannelMetadataResultA detailed object containing the result of the operation.
statusPNStatusA status object with additional information.
PNSetChannelMetadataResult​
Property NameTypeDescription
dataDictionary containing channel metadata
statusPNStatusStatus of the operation

Where each element in data contains a dictionary with channel metadata.

KeyDescription
idchannel metadata id
nameName associated with channel metadata object
descriptionDescription associated with channel metadata object
statusChannels status value
typeChannels type value
customCustom object associated with channel metadata object in form of dictionary containing string to string pairs

Other examples​

Iteratively update existing metadata​
1

Remove channel metadata​

Delete metadata for the specified channel.

Removes the metadata from a specified channel.

Method(s)​

To Remove Channel Metadata you can use the following method(s) in the Python SDK:

1pubnub.remove_channel_metadata() \
2 .channel(String)
channel#
Type: String
Default: no default
Channel name

Sample code​

Synchronous:

1pubnub.remove_channel_metadata() \
2 .channel("channel id") \
3 .sync()

Asynchronous:

1def callback(response, status):
2 pass
3
4pubnub.remove_channel_metadata() \
5 .channel("channel id") \
6 .pn_async(callback)
Returns​

The remove_channel_metadata() operation returns an Envelope which contains the following fields:

FieldTypeDescription
resultPNRemoveChannelMetadataResultA detailed object containing the result of the operation.
statusPNStatusA status object with additional information.
PNRemoveChannelMetadataResult​
Property NameTypeDescription
statusPNStatusStatus of the operation

Channel memberships​

Manage the channels a UUID belongs to: list, set, remove, and manage in bulk.

Get channel memberships​

List channel memberships for a UUID. This does not return subscriptions.

Method(s)​

To Get Channel Memberships you can use the following method(s) in the Python SDK:

1pubnub.get_memberships() \
2 .uuid(String) \
3 .limit(Integer) \
4 .page(PNPage Object) \
5 .filter(String) \
6 .sort(* PNSortKey Object) \
7 .include(MembershipIncludes)
uuid#
Type: String
Default: pubnub.configuration.uuid
Unique UUID Metadata identifier.
If not supplied, then UUID from configuration will be used.
limit#
Type: Integer
Default: 100
The maximum number of objects to retrieve at a time
page#
Type: PNPage
Default: no default
The paging object used for pagination
filter#
Type: String
Default: no default
Expression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined in the filtering reference.
sort#
Type: PNSortKey
Default: no default
List of properties to sort by. Available options are id, name, and updated. Use asc or desc to specify sort direction. For example: {name: 'asc'}
include#
Type: MembershipIncludes
Default: no default
The additional information to include in the membership response.
property of include: total_count#
Type: Boolean
Default: False
Request totalCount to be included in paginated response, which is omitted by default
property of include: custom#
Type: Boolean
Default: False
Indicates whether custom data should be included in the response.
property of include: status#
Type: Boolean
Default: False
Indicates whether the status should be included in the response.
property of include: type#
Type: Boolean
Default: False
Indicates whether the type should be included in the response.
property of include: total_count#
Type: Boolean
Default: False
Indicates whether the total count should be included in the response.
property of include: channel#
Type: Boolean
Default: False
Indicates whether the channel information should be included in the response.
property of include: channel_custom#
Type: Boolean
Default: False
Indicates whether custom data for the channel should be included in the response.
property of include: channel_type#
Type: Boolean
Default: False
Indicates whether the type of the channel should be included in the response.
property of include: channel_status#
Type: Boolean
Default: False
Indicates whether the status of the channel should be included in the response.

Sample code​

Synchronous:

1pubnub.get_memberships() \
2 .include(MembershipIncludes(custom=True, channel=True, channel_custom=True)) \
3 .uuid("Some UUID").sync()

Asynchronous:

1def callback(response, status):
2 pass
3
4pubnub.get_memberships() \
5 .include(MembershipIncludes(custom=True, channel=True, channel_custom=True)) \
6 .uuid("Some UUID").pn_async(callback)
Returns​

The get_memberships() operation returns an Envelope which contains the following fields:

FieldTypeDescription
resultPNGetMembershipsResultA detailed object containing the result of the operation.
statusPNStatusA status object with additional information.
PNGetMembershipsResult​
Property NameTypeDescription
dataList of dictionaries containing memberships metadata
statusPNStatusStatus of the operation
total_countintTotal count of results (if include_total_count was set)
prevPNPage.PreviousPNPage instance to be used if further requests
nextPNPage.NextPNPage instance to be used if further requests

Where each element in data contains a dictionary with membership metadata.

KeyDescription
channelDictionary containing channel metadata (id, name, description, custom)
customCustom object associated with membership in form of dictionary containing string to string pairs

Set channel memberships​

Replace or add memberships for a UUID. Provide channels (optionally with custom data).

Set channel memberships for a UUID.

Method(s)​

To Set Channel Memberships you can use the following method(s) in the Python SDK:

1pubnub.set_memberships() \
2 .channel_memberships([PNChannelMembership]) \
3 .uuid(String) \
4 .limit(Integer) \
5 .page(PNPage) \
6 .filter(String) \
7 .sort(* PNSort Object) \
8 .include(MembershipIncludes)
channelMemberships#
Type: [PNChannelMembership]
Default: no default
Collection of PNChannelMembership to add to membership
uuid#
Type: String
Default: pubnub.configuration.uuid
Unique UUID Metadata identifier.
If not supplied, then UUID from configuration will be used.
limit#
Type: Integer
Default: 100
The maximum number of objects to retrieve at a time
page#
Type: PNPage
Default: no default
The paging object used for pagination
filter#
Type: String
Default: no default
Expression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined in the filtering reference.
sort#
Type: PNSortKey
Default: no default
List of properties to sort by. Available options are id, name, and updated. Use asc or desc to specify sort direction. For example: {name: 'asc'}
include#
Type: MembershipIncludes
Default: no default
The additional information to include in the membership response.
property of include: total_count#
Type: Boolean
Default: False
Request totalCount to be included in paginated response, which is omitted by default
property of include: custom#
Type: Boolean
Default: False
Indicates whether custom data should be included in the response.
property of include: status#
Type: Boolean
Default: False
Indicates whether the status should be included in the response.
property of include: type#
Type: Boolean
Default: False
Indicates whether the type should be included in the response.
property of include: total_count#
Type: Boolean
Default: False
Indicates whether the total count should be included in the response.
property of include: channel#
Type: Boolean
Default: False
Indicates whether the channel information should be included in the response.
property of include: channel_custom#
Type: Boolean
Default: False
Indicates whether custom data for the channel should be included in the response.
property of include: channel_type#
Type: Boolean
Default: False
Indicates whether the type of the channel should be included in the response.
property of include: channel_status#
Type: Boolean
Default: False
Indicates whether the status of the channel should be included in the response.
API limits

To learn about the maximum length of parameters used to set channel membership metadata, refer to REST API docs.

Sample code​

Synchronous:

1some_channel = "somechannel"
2some_channel_with_custom = "somechannel_with_custom"
3
4pubnub.set_channel_metadata() \
5 .channel(some_channel) \
6 .set_name("some name") \
7 .sync()
8
9custom_1 = {
10 "key3": "val1",
11 "key4": "val2",
12}
13
14pubnub.set_channel_metadata() \
15 .channel(some_channel_with_custom) \
show all 34 lines

Asynchronous:

1def callback(response, status):
2 pass
3
4some_channel = "somechannel"
5some_channel_with_custom = "somechannel_with_custom"
6
7pubnub.set_channel_metadata() \
8 .channel(some_channel) \
9 .set_name("some name") \
10 .sync()
11
12custom_1 = {
13 "key3": "val1",
14 "key4": "val2"
15}
show all 37 lines
Returns​

The set_memberships() operation returns an Envelope which contains the following fields:

FieldTypeDescription
resultPNSetMembershipsResultA detailed object containing the result of the operation.
statusPNStatusA status object with additional information.
PNSetMembershipsResult​
Property NameTypeDescription
dataList of dictionaries containing memberships metadata
statusPNStatusStatus of the operation
total_countintTotal count of results (if include_total_count was set)
prevPNPage.PreviousPNPage instance to be used if further requests
nextPNPage.NextPNPage instance to be used if further requests

Where each element in data contains a dictionary with membership metadata.

KeyDescription
channelDictionary containing channel metadata (id, name, description, custom)
customCustom object associated with membership in form of dictionary containing string to string pairs

Remove channel memberships​

Remove memberships for a UUID. Provide the channels to remove.

Method(s)​

To Remove Channel Memberships you can use the following method(s) in the Python SDK:

1pubnub.remove_memberships() \
2 .channel_memberships([PNChannelMembership]) \
3 .uuid(String) \
4 .limit(Integer) \
5 .page(PNPage) \
6 .filter(String) \
7 .sort(* PNSort) \
8 .include_total_count(Boolean) \
9 .include_custom(Boolean) \
10 .include_channel(Integer)
channel_memberships#
Type: [PNChannelMembership]
Default: no default
List of channels (as PNChannelMembership) to remove from membership.
uuid#
Type: String
Default: pubnub.configuration.uuid
Unique UUID Metadata identifier.
If not supplied, then UUID from configuration will be used.
limit#
Type: Integer
Default: 100
The maximum number of objects to retrieve at a time.
page#
Type: PNPage
Default: no default
The paging object used for pagination.
filter#
Type: String
Default: no default
Expression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined in the filtering reference.
sort#
Type: PNSortKey
Default: no default
List of properties to sort by. Available options are id, name, and updated. Use asc or desc to specify sort direction. For example: {name: 'asc'}
include_total_count#
Type: Boolean
Default: False
Whether to include the total count in the paginated response. Default is false.
include_custom#
Type: Boolean
Default: False
Whether to include the custom object in the fetch response.
include_channel#
Type: Integer
Default: no default
The level of channel details to return in the membership. Possible values are defined as constants in ChannelIncludeEndpoint: ChannelIncludeEndpoint.CHANNEL and ChannelIncludeEndpoint.CHANNEL_WITH_CUSTOM

Sample code​

Synchronous:

1pubnub.remove_memberships() \
2 .uuid("some_uuid") \
3 .channel_memberships([PNChannelMembership.channel(some_channel)]) \
4 .include_custom(True) \
5 .include_channel(ChannelIncludeEndpoint.CHANNEL_WITH_CUSTOM) \
6 .sync()

Asynchronous:

1def callback(response, status):
2 pass
3
4pubnub.remove_memberships() \
5 .uuid("some_uuid") \
6 .channel_memberships([PNChannelMembership.channel(some_channel)]) \
7 .include_custom(True) \
8 .include_channel(ChannelIncludeEndpoint.CHANNEL_WITH_CUSTOM) \
9 .pn_async(callback)
Returns​

The remove_memberships() operation returns an Envelope which contains the following fields:

FieldTypeDescription
resultPNRemoveMembershipsResultA detailed object containing the result of the operation.
statusPNStatusA status object with additional information.
PNRemoveMembershipsResult​
Property NameTypeDescription
dataList of dictionaries containing memberships metadata
statusPNStatusStatus of the operation
total_countintTotal count of results (if include_total_count was set)
prevPNPage.PreviousPNPage instance to be used if further requests
nextPNPage.NextPNPage instance to be used if further requests

Where each element in data contains a dictionary with membership metadata.

KeyDescription
channelDictionary containing channel metadata (id, name, description, custom)
customCustom object associated with membership in form of dictionary containing string to string pairs

Manage channel memberships​

Add and remove memberships for a UUID in one request.

Method(s)​

To Manage Channel Memberships you can use the following method(s) in the Python SDK:

1pubnub.manage_memberships() \
2 .uuid(String) \
3 .set([PNChannelMembership>]) \
4 .remove([PNChannelMembership]) \
5 .limit(Integer) \
6 .page(PNPage) \
7 .filter(String) \
8 .sort(* PNSortKey) \
9 .include(MembershipIncludes)
uuid#
Type: String
Default: pubnub.configuration.uuid
Unique UUID Metadata identifier.
If not supplied, then UUID from configuration will be used.
set#
Type: [PNChannelMembership]
Default: no default
List of members PNChannelMembership to add to channel
remove#
Type: [PNChannelMembership]
Default: no default
List of members PNChannelMembership to remove from channel
limit#
Type: Integer
Default: 100
The maximum number of objects to retrieve at a time
page#
Type: PNPage
Default: null
The paging object used for pagination
filter#
Type: String
Default: null
Expression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined in the filtering reference.
sort#
Type: PNSortKey
Default: no default
List of properties to sort by. Available options are id, name, and updated. Use asc or desc to specify sort direction. For example: {name: 'asc'}
include#
Type: MembershipIncludes
Default: no default
The additional information to include in the membership response.
property of include: total_count#
Type: Boolean
Default: False
Request totalCount to be included in paginated response, which is omitted by default
property of include: custom#
Type: Boolean
Default: False
Indicates whether custom data should be included in the response.
property of include: status#
Type: Boolean
Default: False
Indicates whether the status should be included in the response.
property of include: type#
Type: Boolean
Default: False
Indicates whether the type should be included in the response.
property of include: total_count#
Type: Boolean
Default: False
Indicates whether the total count should be included in the response.
property of include: channel#
Type: Boolean
Default: False
Indicates whether the channel information should be included in the response.
property of include: channel_custom#
Type: Boolean
Default: False
Indicates whether custom data for the channel should be included in the response.
property of include: channel_type#
Type: Boolean
Default: False
Indicates whether the type of the channel should be included in the response.
property of include: channel_status#
Type: Boolean
Default: False
Indicates whether the status of the channel should be included in the response.

Sample code​

Synchronous:

1pubnub.manage_memberships() \
2 .uuid("some_uuid") \
3 .set([PNChannelMembership.channel(some_channel)]) \
4 .remove([PNChannelMembership.channel(some_channel_with_custom)]) \
5 .include(MembershipIncludes(custom=True, channel=True, channel_custom=True)) \
6 .sync()

Asynchronous:

1def callback(response, status):
2 pass
3
4pubnub.manage_memberships() \
5 .uuid("some_uuid") \
6 .set([PNChannelMembership.channel(some_channel)]) \
7 .remove([PNChannelMembership.channel(some_channel_with_custom)]) \
8 .include(MembershipIncludes(custom=True, channel=True, channel_custom=True)) \
9 .pn_async(callback)
Returns​

The manage_memberships() operation returns an Envelope which contains the following fields:

FieldTypeDescription
resultPNManageMembershipsResultA detailed object containing the result of the operation.
statusPNStatusA status object with additional information.
PNManageMembershipsResult​
Property NameTypeDescription
dataList of dictionaries containing memberships metadata
statusPNStatusStatus of the operation
total_countintTotal count of results (if include_total_count was set)
prevPNPage.PreviousPNPage instance to be used if further requests
nextPNPage.NextPNPage instance to be used if further requests

Where each element in data contains a dictionary with membership metadata.

KeyDescription
channelDictionary containing channel metadata (id, name, description, custom)
customCustom object associated with membership in form of dictionary containing string to string pairs

Channel members​

Manage the users in a channel: list, set, remove, and manage in bulk.

Get channel members​

List users in a channel. Include user metadata if needed.

Method(s)​

To Get Channel Members you can use the following method(s) in the Python SDK:

1pubnub.get_channel_members() \
2 .channel(String) \
3 .limit(Integer) \
4 .page(PNPage) \
5 .filter(String) \
6 .sort(* PNSortKey) \
7 .include(MemberIncludes)
channel#
Type: String
Default: no default
Channel name
limit#
Type: Integer
Default: 100
The maximum number of objects to retrieve at a time
page#
Type: PNPage
Default: no default
The paging object used for pagination
filter#
Type: String
Default: no default
Expression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined in the filtering reference.
sort#
Type: PNSortKey
Default: no default
List of properties to sort by. Available options are id, name, and updated. Use asc or desc to specify sort direction. For example: {name: 'asc'}
include#
Type: MemberIncludes
Default: no default
The additional information to include in the member response.
property of include: total_count#
Type: Boolean
Default: False
Request totalCount to be included in paginated response, which is omitted by default
property of include: custom#
Type: Boolean
Default: False
Indicates whether custom data should be included in the response.
property of include: status#
Type: Boolean
Default: False
Indicates whether the status should be included in the response.
property of include: type#
Type: Boolean
Default: False
Indicates whether the type should be included in the response.
property of include: total_count#
Type: Boolean
Default: False
Indicates whether the total count should be included in the response.
property of include: user#
Type: Boolean
Default: False
Indicates whether the user ID information should be included in the response.
property of include: user_custom#
Type: Boolean
Default: False
Indicates whether custom data for the user should be included in the response.
property of include: user_type#
Type: Boolean
Default: False
Indicates whether the type of the user should be included in the response.
property of include: user_status#
Type: Boolean
Default: False
Indicates whether the status of the user should be included in the response.

Sample code​

Synchronous:

1pubnub.get_channel_members() \
2 .channel("channel") \
3 .include(MemberIncludes(custom=True, channel=True, user_custom=True)) \
4 .sync()

Asynchronous:

1def callback(response, status):
2 pass
3
4pubnub.get_channel_members() \
5 .channel("channel") \
6 .include(MemberIncludes(custom=True, channel=True, user_custom=True)) \
7 .pn_async(callback)

Returns​

The get_channel_members() operation returns an Envelope which contains the following fields:

FieldTypeDescription
resultPNManageMembershipsResultA detailed object containing the result of the operation.
statusPNStatusA status object with additional information.
PNGetChannelMembersResult​
Property NameTypeDescription
data[]List of dictionaries containing channel members metadata
statusPNStatusStatus of the operation
total_countintTotal count of results (if include_total_count was set)
prevPNPage.PreviousPNPage instance to be used if further requests
nextPNPage.NextPNPage instance to be used if further requests

Where each element in data contains a dictionary with membership metadata.

KeyDescription
uuidDictionary containing UUID metadata (id, name, email, externalId, profileUrl, custom)
customCustom object associated with channel member in form of dictionary containing string to string pairs

Set channel members​

Set users in a channel. Provide UUIDs (optionally with custom data).

Method(s)​

To Set Channel Members you can use the following method(s) in the Python SDK:

1pubnub.set_channel_members() \
2 .channel(String) \
3 .uuids([PNUUID object]) \
4 .limit(Integer) \
5 .page(PNPage) \
6 .filter(String) \
7 .sort(* PNSortKey) \
8 .include(MemberIncludes)
channel#
Type: String
Default: no default
Channel name
uuids#
Type: [PNUUID]
Default: no default
List of members PNUUID to add to channel
limit#
Type: Integer
Default: 100
The maximum number of objects to retrieve at a time
page#
Type: PNPage
Default: null
The paging object used for pagination
filter#
Type: String
Default: null
Expression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined in the filtering reference.
sort#
Type: PNSortKey
Default: no default
List of properties to sort by. Available options are id, name, and updated. Use asc or desc to specify sort direction. For example: {name: 'asc'}
include#
Type: MemberIncludes
Default: no default
The additional information to include in the member response.
property of include: total_count#
Type: Boolean
Default: False
Request totalCount to be included in paginated response, which is omitted by default
property of include: custom#
Type: Boolean
Default: False
Indicates whether custom data should be included in the response.
property of include: status#
Type: Boolean
Default: False
Indicates whether the status should be included in the response.
property of include: type#
Type: Boolean
Default: False
Indicates whether the type should be included in the response.
property of include: total_count#
Type: Boolean
Default: False
Indicates whether the total count should be included in the response.
property of include: user#
Type: Boolean
Default: False
Indicates whether the user ID information should be included in the response.
property of include: user_custom#
Type: Boolean
Default: False
Indicates whether custom data for the user should be included in the response.
property of include: user_type#
Type: Boolean
Default: False
Indicates whether the type of the user should be included in the response.
property of include: user_status#
Type: Boolean
Default: False
Indicates whether the status of the user should be included in the response.
API limits

To learn about the maximum length of parameters used to set channel members metadata, refer to REST API docs.

Sample code​

Synchronous:

1pubnub.set_uuid_metadata() \
2 .uuid("some_uuid") \
3 .set_name("some name") \
4 .sync()
5
6custom_1 = {
7 "key3": "val1",
8 "key4": "val2"
9}
10
11pubnub.set_uuid_metadata() \
12 .uuid("some_uuid_with_custom") \
13 .set_name("some name with custom") \
14 .custom(custom_1) \
15 .sync()
show all 26 lines

Asynchronous:

1def callback(response, status):
2 pass
3
4pubnub.set_uuid_metadata() \
5 .uuid("some_uuid") \
6 .set_name("some name") \
7 .sync()
8
9custom_1 = {
10 "key3": "val1",
11 "key4": "val2"
12}
13
14pubnub.set_uuid_metadata() \
15 .uuid("some_uuid_with_custom") \
show all 29 lines

Returns​

The set_channel_members() operation returns an Envelope which contains the following fields:

FieldTypeDescription
resultPNSetChannelMembersResultA detailed object containing the result of the operation.
statusPNStatusA status object with additional information.
PNSetChannelMembersResult​
Property NameTypeDescription
data[]List of dictionaries containing channel members metadata
statusPNStatusStatus of the operation
total_countintTotal count of results (if include_total_count was set)
prevPNPage.PreviousPNPage instance to be used if further requests
nextPNPage.NextPNPage instance to be used if further requests

Where each element in data contains a dictionary with membership metadata.

KeyDescription
uuidDictionary containing UUID metadata (id, name, email, externalId, profileUrl, custom)
customCustom object associated with channel member in form of dictionary containing string to string pairs

Remove channel members​

Remove users from a channel.

Method(s)​

To Remove Channel Members you can use the following method(s) in the Python SDK:

1pubnub.remove_channel_members() \
2 .channel(String) \
3 .uuids([PNUUID]) \
4 .limit(Integer) \
5 .page(PNPage) \
6 .filter(String) \
7 .sort(* PNSortKey) \
8 .include_total_count(Boolean) \
9 .include_custom(Boolean) \
10 .includeUUID(Integer)
channel#
Type: String
Default: no default
Channel name
uuids#
Type: [PNUUID]
Default: no default
List of members (as PNUUID) to remove from channel
limit#
Type: Integer
Default: 100
The maximum number of objects to retrieve at a time
page#
Type: PNPage
Default: no default
The paging object used for pagination
filter#
Type: String
Default: no default
Expression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined in the filtering reference.
sort#
Type: PNSortKey
Default: no default
List of properties to sort by. Available options are id, name, and updated. Use asc or desc to specify sort direction. For example: {name: 'asc'}
include_total_count#
Type: Boolean
Default: False
Request include_total_count to be included in paginated response, which is omitted by default
include_custom#
Type: Boolean
Default: False
Whether to include the Custom object in the response.
include_uuid#
Type: Integer
Default: no default
The level of uuid metadata details to return in the channel member. Possible values are defined as constants in UUIDIncludeEndpoint: UUIDIncludeEndpoint.UUID and UUIDIncludeEndpoint.UUID_WITH_CUSTOM

Sample code​

Synchronous:

1pubnub.remove_channel_members() \
2 .channel("channel id") \
3 .uuids([PNUUID.uuid(some_uuid)]) \
4 .include_custom(True) \
5 .include_uuid(UUIDIncludeEndpoint.UUID_WITH_CUSTOM) \
6 .sync()

Asynchronous:

1def callback(response, status):
2 pass
3
4pubnub.remove_channel_members() \
5 .channel("channel id") \
6 .uuids([PNUUID.uuid(some_uuid)]) \
7 .include_custom(True) \
8 .include_uuid(UUIDIncludeEndpoint.UUID_WITH_CUSTOM).pn_async(callback)

Returns​

The remove_channel_members() operation returns an Envelope which contains the following fields:

FieldTypeDescription
resultPNRemoveChannelMembersResultA detailed object containing the result of the operation.
statusPNStatusA status object with additional information.
PNRemoveChannelMembersResult​
Property NameTypeDescription
data[]List of dictionaries containing channel members metadata
statusPNStatusStatus of the operation
total_countintTotal count of results (if include_total_count was set)
prevPNPage.PreviousPNPage instance to be used if further requests
nextPNPage.NextPNPage instance to be used if further requests

Where each element in data contains a dictionary with membership metadata.

KeyDescription
uuidDictionary containing UUID metadata (id, name, email, externalId, profileUrl, custom)
customCustom object associated with channel member in form of dictionary containing string to string pairs

Manage channel members​

Add and remove users in a channel in one request.

Method(s)​

To Manage Channel Members you can use the following method(s) in the Python SDK:

1pubnub.manage_channel_members() \
2 .channel(String) \
3 .set([PNUUID]) \
4 .remove([PNUUID]) \
5 .limit(Integer) \
6 .page(PNPage) \
7 .filter(String) \
8 .sort(* PNSortKey) \
9 .include(MemberIncludes)
channel#
Type: String
Default: no default
Channel name
set#
Type: [PNUUID]
Default: no default
List of members PNUUID to add to channel
remove#
Type: [PNUUID]
Default: no default
List of members PNUUID to remove from channel
limit#
Type: Integer
Default: 100
The maximum number of objects to retrieve at a time
page#
Type: PNPage
Default: no default
The paging object used for pagination
filter#
Type: String
Default: no default
Expression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined in the filtering reference.
sort#
Type: PNSortKey
Default: no default
List of properties to sort by. Available options are id, name, and updated. Use asc or desc to specify sort direction. For example: {name: 'asc'}
include#
Type: MemberIncludes
Default: no default
The additional information to include in the member response.
property of include: total_count#
Type: Boolean
Default: False
Request totalCount to be included in paginated response, which is omitted by default
property of include: custom#
Type: Boolean
Default: False
Indicates whether custom data should be included in the response.
property of include: status#
Type: Boolean
Default: False
Indicates whether the status should be included in the response.
property of include: type#
Type: Boolean
Default: False
Indicates whether the type should be included in the response.
property of include: total_count#
Type: Boolean
Default: False
Indicates whether the total count should be included in the response.
property of include: user#
Type: Boolean
Default: False
Indicates whether the user ID information should be included in the response.
property of include: user_custom#
Type: Boolean
Default: False
Indicates whether custom data for the user should be included in the response.
property of include: user_type#
Type: Boolean
Default: False
Indicates whether the type of the user should be included in the response.
property of include: user_status#
Type: Boolean
Default: False
Indicates whether the status of the user should be included in the response.

Sample code​

Synchronous:

1pubnub.manage_channel_members() \
2 .channel("channel id") \
3 .set([PNUUID.uuid(some_uuid)]) \
4 .remove([PNUUID.uuid(some_uuid_with_custom)]) \
5 .include(MemberIncludes(custom=True, channel=True, user_custom=True)) \
6 .sync()

Asynchronous:

1def callback(response, status):
2 pass
3
4pubnub.manage_channel_members() \
5 .channel("channel id") \
6 .set([PNUUID.uuid(some_uuid)]) \
7 .remove([PNUUID.uuid(some_uuid_with_custom)]) \
8 .include(MemberIncludes(custom=True, channel=True, user_custom=True)) \
9 .pn_async(callback)

Returns​

The manage_channel_members() operation returns an Envelope which contains the following fields:

FieldTypeDescription
resultPNManageChannelMembersResultA detailed object containing the result of the operation.
statusPNStatusA status object with additional information.
PNManageChannelMembersResult​
Property NameTypeDescription
data[]List of dictionaries containing channel members metadata
statusPNStatusStatus of the operation
total_countintTotal count of results (if include_total_count was set)
prevPNPage.PreviousPNPage instance to be used if further requests
nextPNPage.NextPNPage instance to be used if further requests

Where each element in data contains a dictionary with membership metadata.

KeyDescription
uuidDictionary containing UUID metadata (id, name, email, externalId, profileUrl, custom)
customCustom object associated with channel member in form of dictionary containing string to string pairs

PNChannelMembership class​

PNChannelMembership is a utility class that exposes two factory methods: channel(channel) constructs a channel membership, and channel_with_custom(channelId, custom) constructs a channel membership with additional custom metadata.

1class PNChannelMembership:
2 __metaclass__ = ABCMeta
3
4 def __init__(self, channel):
5 self._channel = channel
6
7 @staticmethod
8 def channel(channel):
9 return JustChannel(channel)
10
11 @staticmethod
12 def channel_with_custom(channel, custom):
13 return ChannelWithCustom(channel, custom)
14
15
show all 24 lines

PNUUID class​

PNUUID is a utility class that exposes two factory methods: uuid(uuid) constructs a UUID, and uuid_with_custom(channel_id, custom) constructs a UUID with additional custom metadata.

1class PNUUID:
2 __metaclass__ = ABCMeta
3
4 def __init__(self, uuid):
5 self._uuid = uuid
6
7 @staticmethod
8 def uuid(uuid):
9 return JustUUID(uuid)
10
11 @staticmethod
12 def uuid_with_custom(uuid, custom):
13 return UUIDWithCustom(uuid, custom)
14
15
show all 24 lines

Was this page useful?

Last updated on