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
| Parameter | Description |
|---|---|
limitType: IntDefault: 100 | Number of objects to return in response. The default (and maximum) value is 100 (set through the underlying Swift SDK). |
pageType: PubNubHashedPage?Default: nil | Object used for pagination to define which previous or next result page you want to fetch. |
filterType: StringDefault: nil | Expression used to filter the results. Returns only these members whose properties satisfy the given expression. The filter language is defined here. |
sortType: [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
| Parameter | Description |
|---|---|
(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
| Parameter | Description |
|---|---|
limitType: IntDefault: 100 | Number of objects to return in response. The default (and maximum) value is 100 (set through the underlying Swift SDK). |
pageType: PubNubHashedPageDefault: n/a | Object used for pagination to define which previous or next result page you want to fetch. |
filterType: StringDefault: n/a | Expression used to filter the results. Returns only these members whose properties satisfy the given expression. The filter language is defined here. |
sortType: [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
| Parameter | Description |
|---|---|
(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 real-time updates when a Membership object is edited with onUpdated(), or be notified when it is deleted with onDeleted().
You can also use membership.stream.updates() and membership.stream.deletions() for AsyncStream-based equivalents.
For monitoring multiple memberships at once, streamUpdatesOn() remains available.
Membership changes
These methods notify about field changes (metadata, status) for existing memberships, not additions or removals.
Deprecation
streamUpdates() is deprecated. Use onUpdated() and onDeleted() (closure-based) or membership.stream.updates() and membership.stream.deletions() (AsyncStream-based) instead.
Method signature
-
onUpdated()— closure called when the membership metadata changes1membership.onUpdated(
2 callback: @escaping (MembershipImpl) -> Void
3) -> AutoCloseable -
onDeleted()— closure called when the membership is deleted1membership.onDeleted(
2 callback: @escaping () -> Void
3) -> AutoCloseable -
streamUpdatesOn()(static) — monitors multiple memberships1MembershipImpl.streamUpdatesOn(
2 memberships: [MembershipImpl]
3) -> AsyncStream<MembershipImpl>
Input
| Parameter | Description |
|---|---|
callback (in onUpdated) *Type: (MembershipImpl) -> VoidDefault: n/a | Closure called with the updated membership whenever its metadata changes. |
callback (in onDeleted) *Type: () -> VoidDefault: n/a | Closure called when the membership is deleted. |
memberships (in streamUpdatesOn) *Type: [MembershipImpl]Default: n/a | A collection of MembershipImpl objects from which you want to receive updates. |
Output
| Parameter | Description |
|---|---|
AutoCloseable | An object you must retain. When released or closed, the listener stops. |
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.
- Closure
- AsyncStream
1
1
Watch multiple memberships
Get updates on multiple membership objects at once.
- Closure
- AsyncStream
1
1
Check if user is a member
hasMember() checks if a user is a member of the channel without fetching the full membership object.
Method signature
1channel.hasMember(
2 userId: String
3) async throws -> Bool
Input
| Parameter | Description |
|---|---|
userId *Type: StringDefault: n/a | ID of the user to check. |
Output
| Parameter | Description |
|---|---|
Bool | true if the user is a member of the channel, false otherwise. |
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.
Check if support_agent_15 is a member of the support channel.
1
Get single member
getMember() retrieves a single membership for a specific user on the channel.
Method signature
1channel.getMember(
2 userId: String
3) async throws -> MembershipImpl?
Input
| Parameter | Description |
|---|---|
userId *Type: StringDefault: n/a | ID of the user whose membership you want to retrieve. |
Output
| Parameter | Description |
|---|---|
MembershipImpl? | The membership object if the user is a member, nil otherwise. |
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 the membership of support_agent_15 on the support channel.
1
Check if user belongs to channel
isMemberOf() checks if the user has a membership on a specific channel.
Method signature
1user.isMemberOf(
2 channelId: String
3) async throws -> Bool
Input
| Parameter | Description |
|---|---|
channelId *Type: StringDefault: n/a | ID of the channel to check. |
Output
| Parameter | Description |
|---|---|
Bool | true if the user is a member of the channel, false otherwise. |
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.
Check if the user support_agent_15 belongs to the support channel.
1
Get single membership
getMembership() retrieves a single membership for the user on a specific channel.
Method signature
1user.getMembership(
2 channelId: String
3) async throws -> MembershipImpl?
Input
| Parameter | Description |
|---|---|
channelId *Type: StringDefault: n/a | ID of the channel whose membership you want to retrieve. |
Output
| Parameter | Description |
|---|---|
MembershipImpl? | The membership object if the user is a member, nil otherwise. |
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 the membership of user support_agent_15 on the support channel.
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 status: String? = nil,
4 type: String? = nil
5) async throws -> MembershipImpl
Input
| Parameter | Description |
|---|---|
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. |
statusType: StringDefault: nil | Optional status value for the membership. |
typeType: StringDefault: nil | Optional type value for the membership. |
Output
| Parameter | Description |
|---|---|
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
Delete membership
Delete a membership with delete().
Method signature
This method takes the following parameters:
1membership.delete() async throws
Input
This method doesn’t take any parameters.
Output
This method doesn’t return any value.
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.
1