On this page

Manage the user-channel membership relationship

Requires App Context

Enable App Context for your keyset in the Admin Portal.

A Membership entity is created when a user joins or is invited to a channel, and ends when the user leaves.

Get members

Get all members of a channel with getMembers().

Method signature

This method takes the following parameters:

1channel.getMembers(
2 limit: Int? = nil,
3 page: PubNubHashedPage? = nil,
4 filter: String? = nil,
5 sort: [PubNub.MembershipSortField] = []
6) async throws -> (memberships: [MembershipImpl], page: PubNubHashedPage?)

Input

* required
ParameterDescription
limit
Type: Int
Default:
100
Number of objects to return in response. The default (and maximum) value is 100 (set through the underlying Swift SDK).
page
Type: PubNubHashedPage?
Default:
nil
Object used for pagination to define which previous or next result page you want to fetch.
filter
Type: String
Default:
nil
Expression used to filter the results. Returns only these members whose properties satisfy the given expression. The filter language is defined here.
sort
Type: [PubNub.MembershipSortField]
Default:
[]
A collection to specify the sort order. Available options are id, name, and updated. Use asc or desc to specify the sorting direction, or specify null to take the default sorting direction (ascending). For example: {name: "asc"}. Unless specified otherwise, the items are sorted by the last updated date. Defaults to an empty list.

Output

ParameterDescription
(memberships: [MembershipImpl], page: PubNubHashedPage?)
A tuple containing a set of channel members with membership and pagination information indicating the start, end, and total count of the members.

Sample code

Sample code

The code samples in Swift Chat SDK focus on asynchronous code execution.

You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.

List all members of the support channel on the premium support plan.

1

Get membership

Get all channel memberships for a user with getMemberships().

To list all channels, use getChannels() instead.

Method signature

This method takes the following parameters:

1user.getMemberships(
2 limit: Int? = nil,
3 page: PubNubHashedPage? = nil,
4 filter: String? = nil,
5 sort: [PubNub.MembershipSortField] = []
6) async throws -> (memberships: [MembershipImpl], page: PubNubHashedPage?)

Input

* required
ParameterDescription
limit
Type: Int
Default:
100
Number of objects to return in response. The default (and maximum) value is 100 (set through the underlying Swift SDK).
page
Type: PubNubHashedPage
Default:
n/a
Object used for pagination to define which previous or next result page you want to fetch.
filter
Type: String
Default:
n/a
Expression used to filter the results. Returns only these members whose properties satisfy the given expression. The filter language is defined here.
sort
Type: [PubNub.MembershipSortField]
Default:
[]
A collection to specify the sort order. Available options are id, name, and updated. Use asc or desc to specify the sorting direction, or specify null to take the default sorting direction (ascending). For example: {name: "asc"}. Unless specified otherwise, the items are sorted by the last updated date. Defaults to an empty list.

Output

ParameterDescription
(memberships: [MembershipImpl], page: PubNubHashedPage?)
Object containing a set of memberships and pagination information indicating the start, end, and total count of the memberships.

Sample code

Sample code

The code samples in Swift Chat SDK focus on asynchronous code execution.

You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.

Find out which channels the support_agent_15 user is a member of.

1

Get updates

Receive updates when Membership objects are edited:

  • streamUpdates() - monitors a single membership
  • streamUpdatesOn() - monitors multiple memberships
Membership changes

These methods notify about field changes (metadata, status) for existing memberships, not additions or removals.

Both methods return an asynchronous stream producing new values when membership data changes. They subscribe to a channel and add an objects event listener for membership events.

Stream update behavior
  • streamUpdates() returns the updated Membership object on each change (nil if deleted)
  • streamUpdatesOn() returns the complete list of monitored memberships on any change

Method signature

These methods take the following parameters:

  • streamUpdates()

    1membership.streamUpdates() -> AsyncStream<MembershipImpl>
  • streamUpdatesOn() (static)

    1MembershipImpl.streamUpdatesOn(
    2 memberships: [MembershipImpl]
    3) -> AsyncStream<MembershipImpl>

Input

ParameterRequired in streamUpdates()Required in streamUpdatesOn()Description
memberships
Type: [MembershipImpl]
Default:
n/a
No
Yes
A collection of MembershipImpl objects from which you want to receive updates.

Output

ComponentDescription
AsyncStream<MembershipImpl>
An asynchronous stream providing updates on membership changes. It allows for real-time processing as new data becomes available.

Sample code

Sample code

The code samples in Swift Chat SDK focus on asynchronous code execution.

You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.

Get updates on the first user membership.

  • streamUpdates()
1

Get updates on the first page of user memberships.

  • streamUpdatesOn()
1

Update

Update a user's channel membership information with update().

Method signature

This method takes the following parameters:

1membership.update(
2 custom: [String: JSONCodableScalar]
3) async throws -> MembershipImpl

Input

* required
ParameterDescription
custom *
Type: [String: JSONCodableScalar]
Default:
n/a
Any custom properties or metadata associated with the channel-user membership in the form of a JSON. Values must be scalar only; arrays or objects are not supported. App Context filtering language doesn’t support filtering by custom properties.

Output

ParameterDescription
MembershipImpl
Returned (modified) object containing the membership data.

Sample code

Sample code

The code samples in Swift Chat SDK focus on asynchronous code execution.

You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.

Assign the premium-support role to support_agent_15 on the high-priority-incidents channel.

1

Last updated on