App Context API for PubNub Swift Native 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 easily 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 set or removed from the database. Clients can receive these events in real-time and update their front-end application accordingly.

UUID and User ID

PubNubUUIDMetadataBase is deprecated but will continue to work as a typealias for PubNubUserMetadataBase.

User

Get Metadata for All Users

Returns a paginated list of User Metadata objects, optionally including the custom data object for each.

Method(s)

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

allUserMetadata(
include: PubNub.UserIncludeFields = PubNub.UserIncludeFields(),
filter: String? = nil,
sort: [PubNub.ObjectSortField] = [],
limit: Int? = 100,
page: PubNubHashedPage? = Page(),
custom requestConfig: RequestConfiguration = RequestConfiguration(),
completion: ((Result<(users: [PubNubUserMetadata], next: PubNubHashedPage?), Error>) -> Void)?
)
ParameterTypeRequiredDefaultDescription
includePubNub.UserIncludeFieldsOptionalPubNub.UserIncludeFields()Whether to include the custom field in the fetch response
filterString?OptionalnilExpression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined here
sort[PubNub.ObjectSortField]Optional[]List of properties to sort response objects. The following properties are valid for sorting: .id, .name, .type, .status, and .updated.
pagePubNubHashedPage?OptionalPubNub.Page()The paging object used for pagination that allows for cursor-based pagination where the pages are navigated using a cursor, such as a next value.
limitInt?Optional100The number of objects to retrieve at a time
customRequestConfigurationOptionalRequestConfiguration()An object that allows for per-request customization of PubNub Configuration or Network Session
completion((Result<(users: [PubNubUserMetadata], next: PubNubHashedPage?), Error>) -> Void)?OptionalnilThe async Result of the method call

Completion Handler Result

Success

A Tuple containing an Array of PubNubUserMetadata, and the next pagination PubNubHashedPage (if one exists).

public protocol PubNubUserMetadata {
/// The unique identifier of the User
var metadataId: String { get }

/// The name of the User
var name: String { get set }

/// The classification of User
var type: String? { get set }

/// The current state of the User
var status: String? { get set }

/// The external identifier for the object
var externalId: String? { get set }
show all 31 lines
public protocol PubNubHashedPage {
/// The hash value representing the next set of data
var start: String? { get }

/// The hash value representing the previous set of data
var end: String? { get }

/// The total count of all objects withing range
var totalCount: Int? { get }
}
Failure

An Error describing the failure.

Basic Usage

pubnub.allUserMetadata() { result in
switch result {
case let .success(response):
print("The user metadata objects \(response.users)")
if let nextPage = response.next {
print("The next page used for pagination: \(nextPage)")
}
case let .failure(error):
print("Fetch All request failed with error: \(error.localizedDescription)")
}
}

Get User Metadata

Returns metadata for the specified User, optionally including the custom data object for each.

Method(s)

To Get User Metadata you can use the following method(s) in the Swift SDK:

fetchUserMetadata(
_ metadataId: String?,
include: PubNub.UserIncludeFields = PubNub.UserIncludeFields(),
custom requestConfig: RequestConfiguration = RequestConfiguration(),
completion: ((Result<PubNubUserMetadata, Error>) -> Void)?
)
ParameterTypeRequiredDefaultDescription
metadataIdStringYesUnique User Metadata identifier. If not supplied, then it will use the request configuration and then the default configuration
includePubNub.UserIncludeFieldsOptionalPubNub.UserIncludeFields()Whether to include the custom field in the fetch response
customRequestConfigurationOptionalRequestConfiguration()An object that allows for per-request customization of PubNub Configuration or Network Session
completion((Result<PubNubUserMetadata, Error>) -> Void)?OptionalnilThe async Result of the method call

Completion Handler Result

Success

The PubNubUserMetadata object belonging to the identifier.

public protocol PubNubUserMetadata {
/// The unique identifier of the User
var metadataId: String { get }

/// The name of the User
var name: String { get set }

/// The classification of User
var type: String? { get set }

/// The current state of the User
var status: String? { get set }

/// The external identifier for the object
var externalId: String? { get set }
show all 31 lines
Failure

An Error describing the failure.

Basic Usage

pubnub.fetchUserMetadata("TestUser") { result in
switch result {
case let .success(userMetadata):
print("The metadata for `\(userMetadata.metadataId)`: \(userMetadata)")
case let .failure(error):
print("Fetch request failed with error: \(error.localizedDescription)")
}
}

Set User Metadata

Set metadata for a User in the database, optionally including the custom data object for each.

Method(s)

To Set User Metadata you can use the following method(s) in the Swift SDK:

setUserMetadata(
_ metadata: PubNubUserMetadata,
include: PubNub.UserIncludeFields = PubNub.UserIncludeFields(),
custom requestConfig: RequestConfiguration = RequestConfiguration(),
completion: ((Result<PubNubUserMetadata, Error>) -> Void)?
)
ParameterTypeRequiredDefaultDescription
metadataPubNubUserMetadataYesUser Metadata to set.
includePubNub.UserIncludeFieldsOptionalPubNub.UserIncludeFields()Whether to include the custom field in the fetch response.
customRequestConfigurationOptionalRequestConfiguration()An object that allows for per-request customization of PubNub Configuration or Network Session. App Context filtering language doesn’t support filtering by custom properties.
completion((Result<PubNubUserMetadata, Error>) -> Void)?OptionalnilThe async Result of the method call.
API limits

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

Completion Handler Result

Success

The PubNubUserMetadata object belonging to the identifier.

public protocol PubNubUserMetadata {
/// The unique identifier of the User
var metadataId: String { get }

/// The name of the User
var name: String { get set }

/// The classification of User
var type: String? { get set }

/// The current state of the User
var status: String? { get set }

/// The external identifier for the object
var externalId: String? { get set }
show all 31 lines
Failure

An Error describing the failure.

Basic Usage

let johnDoe = PubNubUserMetadataBase(
id: "john-doe",
name: "John Doe",
custom: ["title": "Mr. Manager"]
)

pubnub.setUserMetadata(johnDoe) { result in
switch result {
case let .success(userMetadata):
print("The metadata for `\(userMetadata.metadataId)`: \(userMetadata)")
case let .failure(error):
print("Create request failed with error: \(error.localizedDescription)")
}
}

Remove User Metadata

Removes the metadata from a specified UUID.

Method(s)

To Remove User Metadata you can use the following method(s) in the Swift SDK:

removeUserMetadata(
_ metadataId: String?,
custom requestConfig: RequestConfiguration = RequestConfiguration(),
completion: ((Result<String, Error>) -> Void)?
)
ParameterTypeRequiredDefaultDescription
metadataIdStringYesUnique User Metadata identifier. If not supplied, then it will use the request configuration and then the default configuration
customRequestConfigurationOptionalRequestConfiguration()An object that allows for per-request customization of PubNub Configuration or Network Session
completion((Result<String, Error>) -> Void)?OptionalnilThe async Result of the method call

Basic Usage

pubnub.removeUserMetadata("john-doe") { result in
switch result {
case let .success(metadataId):
print("The metadata has been removed for the identifier `\(metadataId)`")
case let .failure(error):
print("Delete request failed with error: \(error.localizedDescription)")
}
}

Channel

Get Metadata for All Channels

Returns a paginated list of Channel Metadata objects, optionally including the custom data object for each.

Method(s)

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

allChannelMetadata(
include: PubNub.IncludeFields = PubNub.IncludeFields(),
filter: String? = nil,
sort: [PubNub.ObjectSortField] = [],
limit: Int? = 100,
page: PubNubHashedPage? = Page(),
custom requestConfig: RequestConfiguration = RequestConfiguration(),
completion: ((Result<(channels: [PubNubChannelMetadata], next: PubNubHashedPage?), Error>) -> Void)?
)
ParameterTypeRequiredDefaultDescription
includePubNub.IncludeFieldsOptionalPubNub.IncludeFields()Whether to include the custom field in the fetch response
filterString?OptionalnilExpression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined here
sort[PubNub.ObjectSortField]Optional[]List of properties to sort response objects. The following properties are valid for sorting: .id, .name, .type, .status, and .updated.
limitInt?Optional100The number of objects to retrieve at a time
pagePubNubHashedPage?OptionalPubNub.Page()The paging object used for pagination
customRequestConfigurationOptionalRequestConfiguration()An object that allows for per-request customization of PubNub Configuration or Network Session
completion((Result<(channels: [PubNubChannelMetadata], next: PubNubHashedPage?), Error>) -> Void)?OptionalnilThe async Result of the method call

Completion Handler Result

Success

A Tuple containing an Array of PubNubChannelMetadata, and the next pagination PubNubHashedPage (if one exists).

public protocol PubNubChannelMetadata {
/// The unique identifier of the Channel
var metadataId: String { get }

/// The name of the Channel
var name: String { get set }

/// The classification of ChannelMetadata
var type: String? { get set }

/// The current state of the ChannelMetadata
var status: String? { get set }

/// Text describing the purpose of the channel
var channelDescription: String? { get set }
show all 25 lines
public protocol PubNubHashedPage {
/// The hash value representing the next set of data
var start: String? { get }

/// The hash value representing the previous set of data
var end: String? { get }

/// The total count of all objects withing range
var totalCount: Int? { get }
}
Failure

An Error describing the failure.

Basic Usage

pubnub.allChannelMetadata() { result in
switch result {
case let .success(response):
print("The channel metadata objects \(response.channels)")
if let nextPage = response.next {
print("The next page used for pagination: \(nextPage)")
}
case let .failure(error):
print("Fetch All request failed with error: \(error.localizedDescription)")
}
}

Get Channel Metadata

Returns metadata for the specified Channel, optionally including the custom data object for each.

Method(s)

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

fetchChannelMetadata(
_ metadataId: String?,
include: PubNub.ChannelIncludeFields = PubNub.ChannelIncludeFields(),
custom requestConfig: RequestConfiguration = RequestConfiguration(),
completion: ((Result<PubNubChannelMetadata, Error>) -> Void)?
)
ParameterTypeRequiredDefaultDescription
channelStringYesUnique Channel Metadata identifier. If not supplied, then it will use the request configuration and then the default configuration
includePubNub.ChannelIncludeFieldsOptionalPubNub.ChannelIncludeFields()Whether to include the custom field in the fetch response
customRequestConfigurationOptionalRequestConfiguration()An object that allows for per-request customization of PubNub Configuration or Network Session
completion((Result<PubNubChannelMetadata, Error>) -> Void)?OptionalnilThe async Result of the method call

Completion Handler Result

Success

The PubNubChannelMetadata object belonging to the identifier.

public protocol PubNubChannelMetadata {
/// The unique identifier of the Channel
var metadataId: String { get }

/// The name of the Channel
var name: String { get set }

/// The classification of ChannelMetadata
var type: String? { get set }

/// The current state of the ChannelMetadata
var status: String? { get set }

/// Text describing the purpose of the channel
var channelDescription: String? { get set }
show all 25 lines
Failure

An Error describing the failure.

Basic Usage

pubnub.fetchChannelMetadata("TestUser") { result in
switch result {
case let .success(channelMetadata):
print("The metadata for `\(channelMetadata.metadataId)`: \(channelMetadata)")
case let .failure(error):
print("Fetch request failed with error: \(error.localizedDescription)")
}
}

Set Channel Metadata

Set metadata for a Channel in the database, optionally including the custom data object for each.

Method(s)

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

setChannelMetadata(
_ metadata: PubNubChannelMetadata,
include: PubNub.ChannelIncludeFields = PubNub.ChannelIncludeFields(),
custom requestConfig: RequestConfiguration = RequestConfiguration(),
completion: ((Result<PubNubChannelMetadata, Error>) -> Void)?
)
ParameterTypeRequiredDefaultDescription
channelPubNubChannelMetadataYesChannel Metadata to set.
includePubNub.ChannelIncludeFieldsOptionalPubNub.ChannelIncludeFields()Whether to include the custom field in the fetch response.
customRequestConfigurationOptionalRequestConfiguration()An object that allows for per-request customization of PubNub Configuration or Network Session. App Context filtering language doesn’t support filtering by custom properties.
completion((Result<PubNubChannelMetadata, Error>) -> Void)?OptionalnilThe async Result of the method call.
API limits

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

Completion Handler Result

Success

The PubNubChannelMetadata object belonging to the identifier.

public protocol PubNubChannelMetadata {
/// The unique identifier of the Channel
var metadataId: String { get }

/// The name of the Channel
var name: String { get set }

/// The classification of ChannelMetadata
var type: String? { get set }

/// The current state of the ChannelMetadata
var status: String? { get set }

/// Text describing the purpose of the channel
var channelDescription: String? { get set }
show all 25 lines
Failure

An Error describing the failure.

Basic Usage

let johnDoe = PubNubChannelMetadataBase(
id: "john-doe",
name: "John Doe",
custom: ["title": "Mr. Manager"]
)

pubnub.setChannelMetadata(johnDoe) { result in
switch result {
case let .success(channelMetadata):
print("The metadata for `\(channelMetadata.metadataId)`: \(channelMetadata)")
case let .failure(error):
print("Create request failed with error: \(error.localizedDescription)")
}
}

Remove Channel Metadata

Removes the metadata from a specified channel.

Method(s)

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

removeChannelMetadatas(
_ metadataId: String?,
custom requestConfig: RequestConfiguration = RequestConfiguration(),
completion: ((Result<String, Error>) -> Void)?
)
ParameterTypeRequiredDefaultDescription
channelStringYesUnique Channel Metadata identifier. If not supplied, then it will use the request configuration and then the default configuration
customRequestConfigurationOptionalRequestConfiguration()An object that allows for per-request customization of PubNub Configuration or Network Session
completion((Result<PubNubChannelMetadata, Error>) -> Void)?OptionalnilThe async Result of the method call

Basic Usage

pubnub.removeChannelMetadata("john-doe") { result in
switch result {
case let .success(metadataId):
print("The metadata has been removed for the channel `\(metadataId)`")
case let .failure(error):
print("Delete request failed with error: \(error.localizedDescription)")
}
}

Channel Memberships

Get Channel Memberships

The method returns a list of channel memberships for a user. This method doesn't return a user's subscriptions.

Method(s)

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

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

fetchMemberships(
userId: String?,
include: PubNub.MembershipInclude = PubNub.MembershipInclude(),
filter: String? = nil,
sort: [PubNub.MembershipSortField] = [],
limit: Int? = 100,
page: PubNubHashedPage? = Page(),
custom requestConfig: RequestConfiguration = RequestConfiguration(),
completion: ((Result<(memberships: [PubNubMembershipMetadata], next: PubNubHashedPageBase?), Error>) -> Void)?
)
ParameterTypeRequiredDefaultDescription
userIdStringYesUnique User Metadata identifier. If not supplied, then it will use the request configuration and then the default configuration
includePubNub.MembershipIncludeOptionalPubNub.MembershipInclude()Whether to include the custom field in the fetch response
filterString?OptionalnilExpression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined here
sort[MembershipSortField]Optional[]List of properties to sort response objects. The following properties are valid for sorting: .object(.id), .object(.name), .object(.type), .object(.status), .object(.updated), .type, .status, .updated.
limitInt?Optional100The number of objects to retrieve at a time
pagePubNubHashedPage?OptionalPubNub.Page()The paging object used for pagination
customRequestConfigurationOptionalRequestConfiguration()An object that allows for per-request customization of PubNub Configuration or Network Session
completion((Result<(memberships: [PubNubMembershipMetadata], next: PubNubHashedPageBase?), Error>) -> Void)?OptionalnilThe async Result of the method call

Completion Handler Result

Success

A Tuple containing an Array of PubNubMembershipMetadata, and the next pagination PubNubHashedPage (if one exists).

public protocol PubNubMembershipMetadata {
/// The unique identifier of the associated User
var userMetadataId: String { get }

/// The unique identifier of the associated Channel
var channelMetadataId: String { get }

/// The current status of the MembershipMetadata
var status: String? { get set }

/// The current type of the MembershipMetadata
var type: String? { get set }

/// The associated User metadata
var user: PubNubUserMetadata? { get set }
show all 28 lines
public protocol PubNubHashedPage {
/// The hash value representing the next set of data
var start: String? { get }

/// The hash value representing the previous set of data
var end: String? { get }

/// The total count of all objects withing range
var totalCount: Int? { get }
}
Failure

An Error describing the failure.

Basic Usage

pubnub.fetchMemberships(userId: "my_user") { result in
switch result {
case let .success(response):
print("The channel memberships for the userId: \(response.memberships)")
if let nextPage = response.next {
print("The next page used for pagination: \(nextPage)")
}
case let .failure(error):
print("Fetch Memberships request failed with error: \(error.localizedDescription)")
}
}

Set Channel Memberships

Set channel memberships for a User.

Method(s)

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

setMemberships(
userId metadataId: String?,
channels memberships: [PubNubMembershipMetadata],
include: PubNub.MembershipInclude = PubNub.MembershipInclude(),
filter: String? = nil,
sort: [PubNub.MembershipSortField] = [],
limit: Int? = 100,
page: PubNubHashedPage? = Page(),
custom requestConfig: RequestConfiguration = RequestConfiguration(),
completion: ((Result<(memberships: [PubNubMembershipMetadata], next: PubNubHashedPageBase?), Error>) -> Void)?
)
ParameterTypeRequiredDefaultDescription
userIdStringYesUnique User Metadata identifier. If not supplied, then it will use the request configuration and then the default configuration
channels[PubNubMembershipMetadata]YesArray of PubNubMembershipMetadata with the PubNubChannelMetadata or channelMetadataId provided
includePubNub.MembershipIncludeOptionalPubNub.MembershipInclude()Whether to include the custom field in the fetch response
filterString?OptionalnilExpression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined here
sort[PubNub.MembershipSortField]Optional[]List of properties to sort response objects. The following properties are valid for sorting: .object(.id), .object(.name), .object(.type), .object(.status), .object(.updated), .type, .status, and .updated.
limitInt?Optional100The number of objects to retrieve at a time
pagePubNubHashedPage?OptionalPubNub.Page()The paging object used for pagination
customRequestConfigurationOptionalRequestConfiguration()An object that allows for per-request customization of PubNub Configuration or Network Session
completion((Result<(memberships: [PubNubMembershipMetadata], next: PubNubHashedPageBase?), Error>) -> Void)?OptionalnilThe async Result of the method call
API limits

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

Completion Handler Result

Success

A Tuple containing an Array of PubNubMembershipMetadata, and the next pagination PubNubHashedPage (if one exists).

public protocol PubNubMembershipMetadata {
/// The unique identifier of the associated User
var userMetadataId: String { get }

/// The unique identifier of the associated Channel
var channelMetadataId: String { get }

/// The current status of the MembershipMetadata
var status: String? { get set }

/// The current type of the MembershipMetadata
var type: String? { get set }

/// The associated User metadata
var user: PubNubUserMetadata? { get set }
show all 28 lines
public protocol PubNubHashedPage {
/// The hash value representing the next set of data
var start: String? { get }

/// The hash value representing the previous set of data
var end: String? { get }

/// The total count of all objects withing range
var totalCount: Int? { get }
}
Failure

An Error describing the failure.

Basic Usage

let newMembership = PubNubMembershipMetadataBase(
userMetadataId: "my_user",
channelMetadataId: "my_channel"
)

pubnub.setMemberships(
userId: newMembership.userMetadataId,
channels: [newMembership]
) { result in
switch result {
case let .success(response):
print("The channel memberships for the userId: \(response.memberships)")
if let nextPage = response.next {
print("The next page used for pagination: \(nextPage)")
}
show all 19 lines

Remove Channel Memberships

Remove channel memberships for a User.

Method(s)

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

removeMemberships(
userId metadataId: String?,
channels memberships: [PubNubMembershipMetadata],
include: PubNub.MembershipInclude = PubNub.MembershipInclude(),
filter: String? = nil,
sort: [PubNub.MembershipSortField] = [],
limit: Int? = 100,
page: PubNubHashedPage? = Page(),
custom requestConfig: RequestConfiguration = RequestConfiguration(),
completion: ((Result<(memberships: [PubNubMembershipMetadata], next: PubNubHashedPageBase?), Error>) -> Void)?
)
ParameterTypeRequiredDefaultDescription
userIdStringYesUnique User Metadata identifier. If not supplied, then it will use the request configuration and then the default configuration
channels[PubNubMembershipMetadata]YesArray of PubNubMembershipMetadata with the PubNubChannelMetadata or channelMetadataId provided
includePubNub.MembershipIncludeOptionalPubNub.MembershipInclude()Whether to include the custom field in the fetch response
filterString?OptionalnilExpression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined here
sort[PubNub.MembershipSortField]Optional[]List of properties to sort response objects. The following properties are valid for sorting: .object(.id), .object(.name), .object(.type), .object(.status), .object(.updated), .type, .status, and .updated.
limitInt?Optional100The number of objects to retrieve at a time
pagePubNubHashedPage?OptionalPubNub.Page()The paging object used for pagination
customRequestConfigurationOptionalRequestConfiguration()An object that allows for per-request customization of PubNub Configuration or Network Session
completion((Result<(memberships: [PubNubMembershipMetadata], next: PubNubHashedPageBase?), Error>) -> Void)?OptionalnilThe async Result of the method call

Completion Handler Result

Success

A Tuple containing an Array of PubNubMembershipMetadata, and the next pagination PubNubHashedPage (if one exists).

public protocol PubNubMembershipMetadata {
/// The unique identifier of the associated User
var userMetadataId: String { get }

/// The unique identifier of the associated Channel
var channelMetadataId: String { get }

/// The current status of the MembershipMetadata
var status: String? { get set }

/// The current type of the MembershipMetadata
var type: String? { get set }

/// The associated User metadata
var user: PubNubUserMetadata? { get set }
show all 28 lines
public protocol PubNubHashedPage {
/// The hash value representing the next set of data
var start: String? { get }

/// The hash value representing the previous set of data
var end: String? { get }

/// The total count of all objects withing range
var totalCount: Int? { get }
}
Failure

An Error describing the failure.

Basic Usage

let oldMembership = PubNubMembershipMetadataBase(
userMetadataId: "my_user",
channelMetadataId: "my_channel"
)

pubnub.removeMemberships(
userId: oldMembership.userMetadataId,
channels: [oldMembership]
) { result in
switch result {
case let .success(response):
print("The channel memberships for the userId: \(response.memberships)")
if let nextPage = response.next {
print("The next page used for pagination: \(nextPage)")
}
show all 19 lines

Channel Members

Get Channel Members

The method returns a list of members in a channel. The list will include user metadata for members that have additional metadata stored in the database.

Method(s)

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

fetchMembers(
channel: String?,
include: PubNub.MemberInclude = PubNub.MemberInclude(),
filter: String? = nil,
sort: [PubNub.MembershipSortField] = [],
limit: Int? = 100,
page: PubNubHashedPage? = Page(),
custom requestConfig: RequestConfiguration = RequestConfiguration(),
completion: ((Result<(memberships: [PubNubMembershipMetadata], next: PubNubHashedPageBase?), Error>) -> Void)?
)
ParameterTypeRequiredDefaultDescription
channelStringYesUnique Channel Metadata identifier
includePubNub.MemberIncludeOptionalPubNub.MemberInclude()Whether to include the custom field in the fetch response
filterString?OptionalnilExpression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined here
sort[PubNub.MembershipSortField]Optional[]List of properties to sort response objects. The following properties are valid for sorting: .object(.id), .object(.name), .object(.type), .object(.status), .object(.updated), .type, .status, and .updated.
limitInt?Optional100The number of objects to retrieve at a time
pagePubNubHashedPage?OptionalPubNub.Page()The paging object used for pagination
customRequestConfigurationOptionalRequestConfiguration()An object that allows for per-request customization of PubNub Configuration or Network Session
completion((Result<(memberships: [PubNubMembershipMetadata], next: PubNubHashedPageBase?), Error>) -> Void)?OptionalnilThe async Result of the method call

Completion Handler Result

Success

A Tuple containing an Array of PubNubMembershipMetadata, and the next pagination PubNubHashedPage (if one exists).

public protocol PubNubMembershipMetadata {
/// The unique identifier of the associated User
var userMetadataId: String { get }

/// The unique identifier of the associated Channel
var channelMetadataId: String { get }

/// The current status of the MembershipMetadata
var status: String? { get set }

/// The current type of the MembershipMetadata
var type: String? { get set }

/// The associated User metadata
var user: PubNubUserMetadata? { get set }
show all 28 lines
public protocol PubNubHashedPage {
/// The hash value representing the next set of data
var start: String? { get }

/// The hash value representing the previous set of data
var end: String? { get }

/// The total count of all objects withing range
var totalCount: Int? { get }
}
Failure

An Error describing the failure.

Basic Usage

pubnub.fetchMembers(channel: "my_channel") { result in
switch result {
case let .success(response):
print("The user members for the channel: \(response.memberships)")
if let nextPage = response.next {
print("The next page used for pagination: \(nextPage)")
}
case let .failure(error):
print("Fetch Memberships request failed with error: \(error.localizedDescription)")
}
}

Set Channel Members

This method sets members in a channel.

Method(s)

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

setMembers(
channel metadataId: String,
users members: [PubNubMembershipMetadata],
include: PubNub.MemberInclude = PubNub.MemberInclude(),
filter: String? = nil,
sort: [PubNub.MembershipSortField] = [],
limit: Int? = 100,
page: PubNubHashedPage? = Page(),
custom requestConfig: RequestConfiguration = RequestConfiguration(),
completion: ((Result<(memberships: [PubNubMembershipMetadata], next: PubNubHashedPageBase?), Error>) -> Void)?
)
ParameterTypeRequiredDefaultDescription
channelStringYesUnique Channel identifier.
users[PubNubMembershipMetadata]YesArray of PubNubMembershipMetadata with the PubNubUserMetadata or userMetadataId provided
includePubNub.MemberIncludeOptionalPubNub.MemberInclude()Whether to include the custom field in the fetch response
filterString?OptionalnilExpression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined here
sort[PubNub.MembershipSortField]Optional[]List of properties to sort response objects. The following properties are valid for sorting: .object(.id), .object(.name), .object(.type), .object(.status), .object(.updated), .type, .status, and .updated.
limitInt?Optional100The number of objects to retrieve at a time
pagePubNubHashedPage?OptionalPubNub.Page()The paging object used for pagination
customRequestConfigurationOptionalRequestConfiguration()An object that allows for per-request customization of PubNub Configuration or Network Session
completion((Result<(memberships: [PubNubMembershipMetadata], next: PubNubHashedPageBase?), Error>) -> Void)?OptionalnilThe async Result of the method call
API limits

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

Completion Handler Result

Success

A Tuple containing an Array of PubNubMembershipMetadata, and the next pagination PubNubHashedPage (if one exists).

public protocol PubNubMembershipMetadata {
/// The unique identifier of the associated User
var userdMetadataId: String { get }

/// The unique identifier of the associated Channel
var channelMetadataId: String { get }

/// The current status of the MembershipMetadata
var status: String? { get set }

/// The current type of the MembershipMetadata
var type: String? { get set }

/// The associated User metadata
var user: PubNubUserMetadata? { get set }
show all 28 lines
public protocol PubNubHashedPage {
/// The hash value representing the next set of data
var start: String? { get }

/// The hash value representing the previous set of data
var end: String? { get }

/// The total count of all objects withing range
var totalCount: Int? { get }
}
Failure

An Error describing the failure.

Basic Usage

let newMembership = PubNubMembershipMetadataBase(
userMetadataId: "my_user",
channelMetadataId: "my_channel"
)

pubnub.setMembers(
channel: newMembership.channelMetadataId,
users: [newMembership]
) { result in
switch result {
case let .success(response):
print("The user members for the channel: \(response.memberships)")
if let nextPage = response.next {
print("The next page used for pagination: \(nextPage)")
}
show all 19 lines

Remove Channel Members

Remove members from a Channel.

Method(s)

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

removeMembers(
channel metadataId: String,
users members: [PubNubMembershipMetadata],
include: PubNub.MemberInclude = PubNub.MemberInclude(),
filter: String? = nil,
sort: [PubNub.MembershipSortField] = [],
limit: Int? = 100,
page: PubNubHashedPage? = Page(),
custom requestConfig: RequestConfiguration = RequestConfiguration(),
completion: ((Result<(memberships: [PubNubMembershipMetadata], next: PubNubHashedPageBase?), Error>) -> Void)?
)
ParameterTypeRequiredDefaultDescription
channelStringYesUnique Channel identifier.
uuids[PubNubMembershipMetadata]YesArray of PubNubMembershipMetadata with the PubNubUserMetadata or userMetadataId provided
includeMemberIncludeOptionalMemberInclude()Whether to include the custom field in the fetch response
filterString?OptionalnilExpression used to filter the results. Only objects whose properties satisfy the given expression are returned. The filter language is defined here
sort[MembershipSortField]Optional[]List of properties to sort response objects. The following properties are valid for sorting: .object(.id), .object(.name), .object(.type), .object(.status), .object(.updated), .type, .status, and .updated.
limitInt?Optional100The number of objects to retrieve at a time
pagePubNubHashedPage?OptionalPubNub.Page()The paging object used for pagination
customRequestConfigurationOptionalRequestConfiguration()An object that allows for per-request customization of PubNub Configuration or Network Session
completion((Result<(memberships: [PubNubMembershipMetadata], next: PubNubHashedPageBase?), Error>) -> Void)?OptionalnilThe async Result of the method call

Completion Handler Result

Success

A Tuple containing an Array of PubNubMembershipMetadata, and the next pagination PubNubHashedPage (if one exists).

public protocol PubNubMembershipMetadata {
/// The unique identifier of the associated User
var userMetadataId: String { get }

/// The unique identifier of the associated Channel
var channelMetadataId: String { get }

/// The current status of the MembershipMetadata
var status: String? { get set }

/// The current type of the MembershipMetadata
var type: String? { get set }

/// The associated User metadata
var user: PubNubUserMetadata? { get set }
show all 28 lines
public protocol PubNubHashedPage {
/// The hash value representing the next set of data
var start: String? { get }

/// The hash value representing the previous set of data
var end: String? { get }

/// The total count of all objects withing range
var totalCount: Int? { get }
}
Failure

An Error describing the failure.

Basic Usage

let oldMembership = PubNubMembershipMetadataBase(
userMetadataId: "my_user",
channelMetadataId: "my_channel"
)

pubnub.removeMembers(
channel: oldMembership.channelMetadataId,
users: [oldMembership]
) { result in
switch result {
case let .success(response):
print("The user members of the channel: \(response.memberships)")
if let nextPage = response.next {
print("The next page used for pagination: \(nextPage)")
}
show all 19 lines
Last updated on